Playing audio files in WPF
I am trying to play audio files in WPF and I am currently using the following code:
开发者_如何学Python 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?
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.
精彩评论