MediaPlayer object stops playing

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(); } 
+7
c # wpf
source share

No one has answered this question yet.

See related questions:

2105
Deep Cloning Objects
1064
How to sort the <T> list by property in an object
872
Deserialize JSON to a dynamic C # object?
838
How to turn a C # object into a JSON string in .NET
725
How to clean Interop Excel objects correctly?
640
How to create a new instance of an object from a Type
484
How to convert byte [] to stream in C #?
289
The calling thread cannot access this object because another thread belongs to it.
one
UWP MediaPlayer stops playing sounds
-one
The set value is not returned

All Articles