Brian Var
Brian Var

Reputation: 6227

How to play a sound file?

I have found a solution on here to play a sound file in WPF which I extracted into a method and call this method in another method. But the when the PlaySound() is called the sound doesn't play. Does anyone have any insight as to why this is happening? Also the sound files are marked as content, but changing to type resource didn't remedy the problem either?

My method to play a sound:

private void PlaySound()
{
     Uri uri = new Uri(@"pack://application:,,,/Sounds/jabSound.wav");
     var player = new MediaPlayer();
     player.Open(uri);
     player.Play();
}

Then I call the method like this but its doesn't play the sound file, PlaySound();

Upvotes: 13

Views: 34264

Answers (3)

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

It turns out that MediaPlayer does not play the music files in embedded resources, quote from Matthew MacDonald book: Pro WPF 4.5 in C#. Chapter 26:

You supply the location of your file as a URI. Unfortunately, this URI doesn’t use the application pack syntax, so it’s not possible to embed an audio file and play it using the MediaPlayer class. This limitation is because the MediaPlayer class is built on functionality that’s not native to WPF—instead, it’s provided by a distinct, unmanaged component of the Windows Media Player.

Therefore, try setting the local path to the your music file:

private void PlaySound()
{
    var uri = new Uri(@"your_local_path", UriKind.RelativeOrAbsolute);
    var player = new MediaPlayer();

    player.Open(uri);
    player.Play();
}

For workaround, see this link:

Playing embedded audio files in WPF

Upvotes: 13

har07
har07

Reputation: 89285

In addition to @Anatoly's answer, I would suggest to listen to MediaFailed event to check for MediaPlayer failure (such as file not found due to wrong path to your .wav file). MediaPlayer doesn't throw exception if it fails to load the media file, it triggers MediaFailed event instead.

And if you're trying to use relative uri, remember that it means relative to your executable file location. In development phase, it is usually inside bin\debug folder. So path to your .wav file should be "../../Sounds/jabSound.wav".

Uri uri = new Uri("../../Sounds/jabSound.wav", UriKind.Relative);
var player = new MediaPlayer();
player.MediaFailed += (o, args) =>
                      {
                          //here you can get hint of what causes the failure 
                          //from method parameter args 
                      };
player.Open(uri);
player.Play();

Upvotes: 6

Liam McInroy
Liam McInroy

Reputation: 4356

You could also use a SoundPlayer

SoundPlayer player = new SoundPlayer(path);
player.Load();
player.Play();

Pretty self explanatory.

BONUS Here's how to have it loop through asynchronously.

bool soundFinished = true;

if (soundFinished)
{
    soundFinished = false;
    Task.Factory.StartNew(() => { player.PlaySync(); soundFinished = true; });
} 

Opens a task to play the sound, waits until the sound is finished, then knows it is finished and plays again.

Upvotes: 26

Related Questions