2012-03-02 75 views
0

我有一個WPF c#中的列表(每個音頻書)的20章節文件,我需要解析以獲取其長度。獲取MP3文件的長度

我目前循環圍繞以下代碼的效率非常低

MediaElement currentmp3 = new MediaElement(); 

currentmp3.UnloadedBehavior = MediaState.Manual; 
currentmp3.LoadedBehavior = MediaState.Manual; 

currentmp3.MediaOpened += new RoutedEventHandler(currentMp3_MediaOpened); 
currentmp3.Source = new Uri(filename); 
currentmp3.Play(); 
currentmp3.Pause(); 

currentmp3.UpdateLayout(); 
// if i dont add this it doesnt give the time. 
Thread.Sleep(1000); 

if (currentmp3.NaturalDuration.HasTimeSpan) 
    chapter.ChapterLength = currentmp3.NaturalDuration.TimeSpan; 

currentmp3.Stop(); 

最多需要30秒的過程。

關於如何提高性能或任何知道任何可以更快地完成相同工作的本地/第三方utils的任何建議?

+0

此信息可能以文件格式本身保存。也許尋找一個可以讀取ID3標籤的庫。 – eandersson 2012-03-02 15:57:25

+0

請儘量保留標題以外的標籤,例如「C#」。相反,只要把它們放在標籤中。 – 2012-03-02 15:58:31

+0

可能的重複:http://stackoverflow.com/questions/1214040/how-to-get-the-length-of-a-mp3-in-c-sharp – vulkanino 2012-03-02 15:58:36

回答