MediaPlayer在Visual Studio中导致AppCrash

时间:2014-05-03 13:24:34

标签: c# media-player crash

当我在visual studio调试器中启动我的应用程序时,它会停止工作"使用AppCrash:

  Fault Module Name:    KERNELBASE.dll
  Exception Code:   e0455858
  Exception Offset: 0000c41f

我已将它缩小到我的SoundPlayer,它继承了MediaPlayer并添加了循环和播放状态。当我评论它时,应用程序正常关闭,我尝试添加dispose以查看在应用程序关闭之前是否需要停止声音,但这并没有解决它。

应该注意的是,应用程序只在Visual Studio调试器中崩溃,如果我只运行可执行文件就可以正常关闭。

using System;

namespace GameEngine
{
    public class SoundPlayer : System.Windows.Media.MediaPlayer, IDisposable
    {
        private bool Loop = false;
        public bool Playing { private set; get; }

        public SoundPlayer(string url)
        {
            this.Open(new System.Uri(System.IO.Directory.GetCurrentDirectory() + "/" + url));
            this.MediaEnded += (sender, e) => 
            {
                this.Playing = false;
                if (this.Loop)
                {
                    this.ResetTime();
                }
            };
        }

        public void PlayOnce()
        {
            this.StartPlaying();
        }

        public void PlayLooping()
        {
            this.Loop = true;
            this.StartPlaying();
        }

        public void StopPlaying()
        {
            this.Stop();
            this.Playing = false;
        }

        public void StopLooping()
        {
            this.Loop = false;
        }

        private void StartPlaying()
        {
            if (!this.Playing)
            {
                this.Playing = true;
                this.ResetTime();
                this.Play();
            }
        }

        private void ResetTime()
        {
            this.Position = new TimeSpan(0);
        }

        public void Dispose()
        {
            this.Stop();
            this.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

修正:

public void Dispose()
{
   this.Stop();
   this.Close();
   this.Dispatcher.InvokeShutdown();
}

在Dispose方法中添加了this.Dispatcher.InvokeShutdown();