I have a WPF Caliburn.Micro application, and I use MediaPlayer class to play audio. I implemented Play, Stop, and Pause functionality, but I don’t see a method for Resume (after Pause) in MediaPlayer. Could you please help me with this?
Here is some of my code:
public void Play()
{
try
{
var audio = Tpv.GetAudio(SelectedTpv.TpvId);
var file = Path.GetTempFileName().Replace(".tmp", ".wma");
File.WriteAllBytes(file, audio);
Player.Open(new Uri(file, UriKind.Absolute));
Player.Play();
IsPlaying = true;
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Failed to play audio:\n{0}", ex.Message), "Failure",
MessageBoxButton.OK, MessageBoxImage.Error);
Console.WriteLine(ex.Message);
}
}
Thanks.
I’m pretty sure that
Playis also supposed to handle resume functionality. According to the MSDN for System.Windows.Media.MediaPlayer thePlaymethod is supposed to “Play media from the current Position”. This means that when you are playing media from the beginning, the position is 0. If you pause, then the media will be paused at a certain position. Pressing play again should resume playback from the same position that you paused the media on.Edit:
Based on the code update you provided, it looks like your issue is that you are loading the file each time you click play. This would cause any previous pause information to be erased, and would treat the file as being brand new each time. You should put some sort of check in there to say that if the file is not already loaded, then load it. Otherwise, your
Playmethod should just callPlayer.Play()to resume.I would also note that you would need to also call
Player.Closewhen you switch the selected item. This would let thePlaymethod know that it needs to load a different file.