How to play all songs from the <Song> list using Windows Media Player?
so I have a list class SąrašasList and it has a string element called vardas that contains the name of the path to the song, I have several songs in this list, as well as a graphical interface 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();
}
}
He plays only the last song, but does not move on to another, what should I do? I want this player to play all the songs one by one.
+4
1 answer
, , , . , , .
, , n + 1.
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 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
}
+5