79368146

Date: 2025-01-18 23:53:04
Score: 2.5
Natty:
Report link

Once the media is loaded the MediaElement.CanPause tells whether the MediaElement supports being paused, it does not change when the MediaElement.Play() or MediaElement.Pause() methods are called.

To get your code working you could introduce a bool field representing the state of the music media element:

private MediaElement music;
private bool isMusicPlaying = false;

Then use it to determine whether to play or pause and set the state accordingly:

if (isMusicPlaying)
{
    music.Pause();
    isMusicPlaying = false;
}
else
{
    music.Play();
    isMusicPlaying = true;
}

Using the actual state of the media element would be a more robust solution. The System.Windows.Controls MediaElement does not expose any public property telling its current state, but you can get it using reflection as shown in this answer:

How do I determine if MediaElement is playing?

Reasons:
  • Blacklisted phrase (1): How do I
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Pekkasso