MediaPlayer对象停止播放

dKo*_*sec 8 c# wpf

我的应用程序中的MediaPlayer对象问题已经有一段时间了.基本上会发生什么:声音播放一段时间然后突然停止.这是我使用的两种方法.

protected virtual void playMovingSound()
{
    movingSound.Open(new Uri(@"Music\walk.mp3", UriKind.Relative));
    movingSound.Volume = 0.6 * ((GameVariables.ingameSoundOn) ? 1 : 0);
    movingSound.Play();
}

protected void stopMovingSound()
{
    movingSound.Stop();
}
Run Code Online (Sandbox Code Playgroud)

我不明白问题是什么.即使我在播放声音之前调用MediaPlayer的构造函数,问题仍然存在.

此外,MediaPlayer的其他实例也会同时停止播放.

方法stopMovingSound()和playMovingSound()每秒触发一次.

Edit1:类构造函数如下所示:

protected MediaPlayer movingSound = null;
public PlayerControlledObject(some params...) : base() 
{
    //...
    movingSound = new MediaPlayer();
}
Run Code Online (Sandbox Code Playgroud)

和makeStep方法一样

public virtual void makeStep(double stepUnit)
{
    double loopSteps = 100;
    double littleStepX, littleStepY;
    littleStepX = (angle == 0 ? 1 : angle == 180 ? -1 : 0) * stepUnit / loopSteps;
    littleStepY = (angle == 90 ? 1 : angle == 270 ? -1 : 0) * stepUnit / loopSteps;

    Direction currentDirection = getDirection(angle);
    if (!isWalkable(currentDirection))
    {
        return;
    }
    playMovingSound();
    animationRunning = true;
    new Thread(new ThreadStart(() =>
        {
            for (; loopSteps >= 0; loopSteps--)
            {
                Dispatcher.Invoke(new Action(() =>
                {
                    moveTo(GetLeft() + littleStepX, GetTop() + littleStepY);
                }));
                Thread.Sleep(10);
            }
            Dispatcher.Invoke(new Action(() =>
            {
                stopMovingSound();
                fixOffsetAndCenterPlayerPosition();
            }));
            animationRunning = false;
        })) { IsBackground = true }.Start();
}
Run Code Online (Sandbox Code Playgroud)