vexe
vexe

Reputation: 5615

Issue with getting the duration of an audio file using the MediaPlayer class

I'm working with some Audio files in my app (mp3, wav, ..etc) I was using the MediaPlayer class from the System.Windows.Media namespace.. but I had some problems with computing the duration of the sound file, my form is actually a small media player, it has a TrackBar a ComboBox and the normal next, previous and play buttons .. I used this code to get the duration of the sound file to determine the maximum value of the track bar:

private void MusicComboBox_SelectedIndexChanged(object sender, EventArgs e)
{   
  System.Windows.Duration duration = Player.NaturalDuration;
  SeekBar.Value = 0;
  Player.Open(new Uri(soundEffectPackage.GetMusicAt(MusicComboBox.SelectedIndex)));
  if(duration.HasTimeSpan)
    SeekBar.Maximum = duration.TimeSpan.Seconds;  
}

This is working fine with only some of the files, while other files don't have a TimeSpan so the if isn't getting executed, and if i removed the if, I'll get an exception saying that I should check first to see if the HasTimeSpan is true, then moving on.

How can i fix this? How can i get the duration of the audio file ? And what do they mean by a time span ?

Any help would be appreciated, thank you :)

Upvotes: 0

Views: 1541

Answers (1)

KF2
KF2

Reputation: 10153

According This

You need to wait for MediaOpened event to fire, NaturalDuration will be available after that. To check if value is available, you can use NaturalDuration.HasTimeSpan property.

but the best chose for working with audio and video file is "Fmod.dll".this have a lot of Privilege to work with audio file

Upvotes: 1

Related Questions