Play audio files in WPF

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 works well, except that it does not produce sound. What am I doing wrong?

+1
source share
1 answer

Your MediaPlayer object probably collects garbage before it can play the file, since it has a local area. Try making the media player an object, a member variable of the class with the application lifetime and see if it fixes it.

+6
source

Source: https://habr.com/ru/post/1339651/


All Articles