0

I want to play all of mp3 from List variable but it only play the last mp3 file.

This is my code

private void button2_Click(object sender, EventArgs e)
{
    List<String> voiceEN = new List<string>();
    WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();

    voiceEN = NumberToWordEN(4515);


    foreach (string i in voiceEN)
    {
        wplayer.URL = "voicesEN\\" + i + ".mp3";
        wplayer.controls.play();

        Console.WriteLine("voicesEN\\" + i + ".mp3");
    }
}

when I run this code it show like this in output

voicesTH\five.mp3
voicesTH\thousand.mp3
voicesTH\five.mp3
voicesTH\ten.mp3
voicesTH\1sp.mp3

But it play 1sp.mp3 only. How to play all of these mp3 .

MrWombat
  • 649
  • 9
  • 19
user572575
  • 1,009
  • 3
  • 25
  • 45
  • `Play` starts playing a file, it doesn't wait for it to end. You can't play all files with a *single* player. What exactly did you expect to happen? – Panagiotis Kanavos Dec 18 '14 at 10:57
  • Have you tried moving `WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();` inside the `foreach` loop? – Andy Refuerzo Dec 18 '14 at 10:58
  • You don't wait for the mp3s to end use the `PlayStateChanged` event to determine if a mp3 has ended. – Sybren Dec 18 '14 at 10:59

3 Answers3

2

Looks to me like you keep telling it to play a new track, so each new track replaces the previous one, leaving it with the last one playing in the end.

Don't know how to use the library you're using, but I'm guessing you need to do one of the following:

  • Either use a playlist feature of the library
  • Or play a single file and subscribe to some sort of "done playing event" where you start playing the next track when the previous one finishes.
Svish
  • 152,914
  • 173
  • 462
  • 620
  • I try to fix by use funtion wplayer_PlayStateChange and tmr_Tick like this [link]http://stackoverflow.com/questions/17644754/how-to-detect-when-a-mp3-file-has-finished-playing[link] But it's loop last file 1st.mp3 only. It not run every file. How to fix it. – user572575 Dec 18 '14 at 18:52
  • Add your files to a Queue. Start playing the first item. Subscribe to PlayStateChange and when you get the `WMPLib.WMPPlayState.wmppsStopped` mentioned by @mrwombat, start playing the next file in the queue. Repeat until queue is empty. – Svish Dec 19 '14 at 12:43
0

As @Svish and @Sybren said, register to the event when the playback is complete:

If you are using the WindowsMediaPlayer library, use the PlayStateChange event and check for WMPLib.WMPPlayState.wmppsStopped

MrWombat
  • 649
  • 9
  • 19
0

Thank you . Now I can play all mp3 from List by add all mp3 to playlist . WMPLib.IWMPPlaylist wplayerList;

user572575
  • 1,009
  • 3
  • 25
  • 45