WPF MediaElement如何从暂停的地方恢复播放?

时间:2013-07-10 02:54:35

标签: c# wpf audio mediaelement

我有2个按钮,播放和暂停,当我点击暂停时,音乐停止,当我点击播放时,它从头开始播放音频。我想这样做,就像我按下播放,它从我停止的地方恢复。

        private void PlayAudio()
    {


        McMediaElement.LoadedBehavior = MediaState.Manual;
        McMediaElement.Source = new Uri("../../SingAlong/Food Fit For A King/old king cole.mp3", UriKind.RelativeOrAbsolute);
        McMediaElement.Play();
    }

    private void button1_Click_1(object sender, RoutedEventArgs e)
    {
        PlayAudio();

    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        McMediaElement.Pause();
    }

2 个答案:

答案 0 :(得分:2)

设置PlayAudio()属性时,Source方法会重新加载媒体文件。这会导致您的对象在调用Play()时从头开始播放新加载的媒体。不应在事件处理程序button1_Click_1中执行此操作,而应仅调用Play()方法:

private void button1_Click_1(object sender, RoutedEventArgs e)
{
    McMediaElement.Play();
}

答案 1 :(得分:0)

这对我有用..

private void button1_Click_1(object sender, RoutedEventArgs e)
{
   McMediaElement.LoadedBehavior = MediaState.Pause;
}
private void button2_Click_1(object sender, RoutedEventArgs e)
{
   McMediaElement.LoadedBehavior = MediaState.Play;
}