MediaPlayer对象停止播放

时间:2015-01-02 13:47:11

标签: 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();
}

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

MediaPlayer的其他实例也会同时停止播放。

每秒触发方法stopMovingSound()和playMovingSound()。

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

protected MediaPlayer movingSound = null;
public PlayerControlledObject(some params...) : base() 
{
    //...
    movingSound = new MediaPlayer();
}

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();
}

0 个答案:

没有答案
相关问题