21 Jun

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.

Sample Usage:

            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.

Add Windows Media Player Reference

Sample Usage:

            var mediaPlayer = new MediaPlayer.MediaPlayer();
            mediaPlayer.FileName = @"C:\Test\CSharpExamplesSound.mp3";

2 thoughts on “Play Mp3 And Wav Files As Synchronous And Asynchronous In C#

  1. I use media player in a thread but mp3 is play only 1 second again and again.How can i play mp3 continuously

    private void mp3Cal()
    Player.URL = filedialog.FileName;

    }//Player is Media Player’s name

    private void timer1_Tick(object sender, EventArgs e)
    Thread th1 = new Thread(new ThreadStart(StartSlider));
    Thread th2 = new Thread(new ThreadStart(mp3Cal));


Leave a Reply

Your email address will not be published. Required fields are marked *