Click here to Skip to main content
15,039,525 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can I play an mp3 song straight after the previous mp3 song has finished? I've checked everywhere but they aren't detailed enough. I'm using WMPLib. How can I change this code so that I can play another song straight after the previous song has come to an end.
#region Playing Music
private void button1_Click(object sender, EventArgs e)
    musicplayer.URL = @"C:\Users\User\Documents\Music\Song1.mp3";;
Updated 18-Jul-15 10:39am
[no name] 18-Jul-15 17:24pm
Coder1999 19-Jul-15 5:57am
I'm doing it in c# not c++
[no name] 19-Jul-15 12:03pm
So what? Then use C#. If you have actually looked at the documentation. you would have seen C# example code.

try this code. its working i have tested
include windows media player library before using the below code
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Audio Files (.mp3)|*.mp3";
if(dialog.ShowDialog() == DialogResult.OK)
    string path = dialog.FileName;
    WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = path;;
Coder1999 19-Jul-15 5:53am
I have seen that before but it didn't help.
Something like this should do the Job:

WindowsMediaPlayer musicplayer = new WindowsMediaPlayer();
// Add Event handler
musicplayer.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(MusicPlayer_StatusChange);

// The eventhandler
private void MusicPlayer_StatusChange(int NewState)
    if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
       // Start next

I think with WindowsMediaPlayer's PlayList you can solve it mutch more elegant.

Coder1999 19-Jul-15 9:38am
What do you put where it says //start new
[no name] 19-Jul-15 10:02am
// Next filename, of course not fixed programmed, should come from a list
musicplayer.URL = @"C:\Users\User\Documents\Music\Song2.mp3";
// Play;

So the same you have in your button click.
Coder1999 19-Jul-15 10:25am
Ok. But then all the songs will play at the same time won't they?
[no name] 19-Jul-15 10:33am
You need to implement a list of strings from where you take the next song.

musicplayer.URL = myPlayList[listIndex++];

and course you Need to check that "listIndex" is in the range.
Coder1999 19-Jul-15 10:55am
Wouldn't it be easier if i made a string for each song then list all the string variables into the musicplayer.ToString and then add a loop after that.
[no name] 19-Jul-15 11:07am
Have a look to this, I think Answer#6 will solve your request:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900