I have been having problems with the MediaPlayer object in the application for quite some time. The main thing that happens: the sound plays well for some time, and then it suddenly stops. These are the two methods that I use.
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(); }`
I do not understand what the problem is. Even if I call the MediaPlayer constructor before playing the sound, the problem still persists.
Other instances of MediaPlayer also stop playback at the same time.
The stopMovingSound () and playMovingSound () methods run every second.
Edit1: The constructor of the class is as follows:
protected MediaPlayer movingSound = null; public PlayerControlledObject(some params...) : base() { ... ... ... movingSound = new MediaPlayer(); }
And the makeStep method
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(); }
c # wpf
dKorosec
source share