1

I am trying to play audio files in WPF and I am currently using the following code:

    FileTextBox.Text = selectedFileName;
    MediaPlayer mp = new MediaPlayer();
    mp.Open(new Uri(selectedFileName, UriKind.Relative ));
    mp.Play();

It's working well, except that it doesn't plays the sound. What am I doing wrong?

Spen D
  • 3,745
  • 9
  • 33
  • 44

1 Answers1

6

Your MediaPlayer object is probably being garbage collected before it has a chance to play the file because it has local scope. Try making the media player object a member variable of a class that has application lifetime and see if this fixes it.

Stu Mackellar
  • 11,292
  • 1
  • 36
  • 59