3

so I have a list class called SąrašasList and it has string item called vardas, which contains a path name to a song, I have few songs in this list, and also GUI with AX Windows Media player, but when I write :

private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
    foreach (SąrašasList d in atrinktas)
    {
        axWindowsMediaPlayer1.URL = d.getVardas();
    }
}

It only plays the last song, but does not go to another, what should I do? I want this player to play all songs, one after the other.

Haby
  • 35
  • 10

1 Answers1

5

Basically, what you're doing is iterating through all of your songs, and loading each one into the player. Of course, your player can only handle one of them, so the last one of your collection is effectively playing.

What you should do is loading your first song, and at the end of each media, you have to load the n + 1 song.

private System.Timers.Timer Timer; // Used to introduce a delay after the MediaEnded event is raised, otherwise player won't chain up the songs

private void ScheduleSongs() {
    var count = 0;
    var firstSong = atrinktas.FirstOrDefault(); // using Linq
    if(firstSong == null) return;
    axWindowsMediaPlayer1.URL = firstSong.getVardas();
    // PlayStateChange event let you listen your player's state. 
    // https://msdn.microsoft.com/fr-fr/library/windows/desktop/dd562460(v=vs.85).aspx
    axWindowsMediaPlayer1.PlayStateChange += delegate(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) { 
       if(e.newState == 8 && count < atrinktas.Count()) {
          count++;
          var nextSong = atrinktas[count];
          axWindowsMediaPlayer1.URL = nextSong.getVardas();
          Timer = new System.Timers.Timer() { Interval = 100 };
          Timer.Elapsed += TimerElapsed; // Execute TimerElapsed once 100ms is elapsed          
       } 
    };
}

private void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    Timer.Stop();
    Timer.Elapsed -= TimerElapsed;
    Timer = null;

    axWindowsMediaPlayer1.Ctlcontrols.play(); // Play the next song
}
  • Now the part : "sender, PlayStateChangeEvent e " gets red – Haby Jan 09 '16 at 11:55
  • And now the same part + e.newState get red – Haby Jan 09 '16 at 12:00
  • Updated (deleted lambda and add ; at the end) –  Jan 09 '16 at 12:15
  • PlayStateChangeEvent e gets red, "The type or namespace PlayStateChangeEvent could not be found". I don't understand, should I declarate in somewhere or what? :/ – Haby Jan 10 '16 at 11:25
  • Well, replace PlayStateChangeEvent by **AxWMPLib._WMPOCXEvents_PlayStateChangeEvent** (I'm sorry, I'm not able to check my code right now) –  Jan 10 '16 at 11:47
  • Now, in the line var firstSong = atrinktas.First(); I get Exception thrown: 'System.InvalidOperationException' in System.Core.dll, Additional information: Sequence contains no elements; I think, that I should mention, that when I open my program, I choose songs from other list, and then they get from one list to "atrinktas" list, so in the begining, there are no members in "atrinktas" list, maybe that is the problem? Maybe I can make player play songs from full list, and when I fill new "atrinktas" list I can play from this? – Haby Jan 10 '16 at 12:28
  • I just tried to use main list and player worked, but how now I can made player to check if the new list is filled and play from it? – Haby Jan 10 '16 at 12:40
  • I see. So all the code I've written should be called only when your song list gets populated. I do not have access to the rest of your code, but you should call what I've written just after adding your elements to the list. I've also modified my code to make it more robust (using FirstOrDefault()) –  Jan 10 '16 at 12:43
  • Thank you! This worked! maybe you also know how to make player play songs one after another without pressing play when first song finished? – Haby Jan 10 '16 at 12:50
  • I've added `axWindowsMediaPlayer1.Ctlcontrols.play();` statement to the code, so that it automatically plays the next song. –  Jan 10 '16 at 12:56
  • Did not helped, next song does not play without pressing play button :/ – Haby Jan 10 '16 at 12:59
  • Hmm, I guess we should wait a little bit after media ended before playing the next one. I've updated the code to make a 100ms sleep. Don't sure it will solve the problem but it's worth checking –  Jan 10 '16 at 13:06
  • This also dont. Media player just write ready and I have to press play button. There is another problem, when last song ends, there are no more in the list, so error (exception) comes, how to avoid this? I think I need my program just wait till I fill with new songs? – Haby Jan 10 '16 at 13:26
  • I've added a timer which *should* play the next song, and I modified the indexer to fix the exception issue. –  Jan 10 '16 at 14:08
  • Error at "e.newState == WMPLib.WMPPlayState.wmppsMediaEnded", because first seems to be "int" and second "WMPPlayState", they cant be equal. How to fix this? :) – Haby Jan 11 '16 at 09:57
  • Haha, never ending! Fixed, rollback to e.newState == 8 –  Jan 11 '16 at 10:14
  • Where should I put this? Previous code were in private void button1_Click(object sender, EventArgs e) {} but tis one does not seem to fit here? Or maybe I mixed something? – Haby Jan 11 '16 at 14:29