0

I could play wav audio file in Resources with the code below.

The files were added to Resources by:

Practice_AccessResource > File > Properties > Resources > Audio > Add > ExistingFiles

//only can play audio file with SoundPlayer class.
System.IO.Stream strForMusic = Practice_AccessResourceFile.Properties.Resources.TaDa;
System.Media.SoundPlayer sountPlayer = new 
System.Media.SoundPlayer(strForMusic);
sountPlayer.Play();

FYI, the project name is the "Practice_AccessResourceFile", and name of wav audio file is TaDa.wav , sorry that I could not change the name for u.

Is there any way to play audio file with MediaPlayer in WPF?

System.IO.Stream stream = Properties.Resources.TaDa;
MediaPlayer musicPlayer = new System.Windows.Media.MediaPlayer();
//musicPlayer.Open(new System.Uri(stream));  //This is not working
//musicPlayer.Play();

I have to play two music simultaneously, and it's not possible with SoundPlayer.

Can anyone help me play audio file with MediaPlayer class in WPF?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Jay. K
  • 52
  • 2
  • 10

1 Answers1

1

I believe MediaPlayer cannot play from Resources, see this question for example.

If you can write the files to a temporary folder on disk then it should be be possible, per this answer:

var p1 = new System.Windows.Media.MediaPlayer();
p1.Open(new System.Uri(@"C:\windows\media\tada.wav"));
p1.Play();

// this sleep is here just so you can distinguish the two sounds playing simultaneously
System.Threading.Thread.Sleep(500);

var p2 = new System.Windows.Media.MediaPlayer();
p2.Open(new System.Uri(@"C:\windows\media\tada.wav"));
p2.Play();
danielmcn
  • 390
  • 6
  • 15
  • The code you posted is what I already know, but thanks that u tell me it's impossible with MediaPlayer in normal way. I guess I will just create the file in temp folder locally. – Jay. K Oct 09 '17 at 14:52