Play Mp3 And Wav Files As Synchronous And Asynchronous In C#
Play A Wav File
SoundPlayer class in System.Media namespace controls playback of a sound from a .wav file. To play the Wav file, you should create a new SoundPlayer object. Then you should set the SoundLocation property to the location of your WAV file, and then use the Play method to play it.
System.Media.SoundPlayer player = new System.Media.SoundPlayer(); player.SoundLocation = @"C:\Test\CSharpExamplesSound.wav"; player.Play(); //in another thread player.PlaySync(); //in same thread
“Play” method in SoundPlayer class plays the .wav file using a new thread. But “PlaySync” method in SoundPlayer plays thw .wav file in same thread.
Play a MP3 File
Above example only plays the .wav files. If you want to play “.mp3” or any other files are supported by windows media player, you can use MediaPlayer class. To use this class, you must add a reference to MedialPlayer.
var mediaPlayer = new MediaPlayer.MediaPlayer(); mediaPlayer.FileName = @"C:\Test\CSharpExamplesSound.mp3"; mediaPlayer.Play();