T
Tonci
Hello everyone,
I'm writing small c# media player app. Situation is like this:
I want it to read playlist from file (works great).
I want it to remove song once that song is finished playing (I have
problems with this one).
My code is here:
private void Form1_Load(object sender, EventArgs e)
{
axPlayer.PlayStateChange += new
AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axPlayer_PlayStateChange);
string myPlaylist = "moja_zadnja"; // playlist name
//WMPLib.IWMPPlaylist pl;
WMPLib.IWMPPlaylistArray plItems;
plItems = axPlayer.playlistCollection.getByName(myPlaylist);
//pl = plItems.Item(0);
if (plItems.count == 0)
{
// MessageBox.Show("COUNT = 0");
pl = axPlayer.playlistCollection.newPlaylist(myPlaylist);
}
else
{
//MessageBox.Show("COUNT != 0");
pl = plItems.Item(0);
}
axPlayer.settings.autoStart = true; //not necessary
axPlayer.currentPlaylist = pl; //Set media player to use
the playlist.
axPlayer.Ctlcontrols.play(); // start playing
}
private void axPlayer_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)//Media stopped
{
// state 8 occurs when going to next song so I used that
IWMPMedia trenutni_medij =
axPlayer.currentPlaylist.Item[0]; // current media
axPlayer.currentPlaylist.removeItem(trenutni_medij);
// problem is that state 8 occurst when removing media, so situation
is like this, it plays first song, then doesn't play second, then plays
third and since then, everything is ok.
}
else if (e.newState == 1)
{
axPlayer.Ctlcontrols.play();
// I put this here because when first song is played, // mediaplayer
stops (goes to state 1)
//downside of this else if is that it replays last song. But when I
remove else if (e.newState == 1) then mediaplayer stops after first song.
}
MSDN here
http://msdn.microsoft.com/en-us/library/windows/desktop/dd562460(v=vs.85).aspx
states:
"Remarks
Windows Media Player states are not guaranteed to occur in any
particular order. Furthermore, not every state necessarily occurs during
a sequence of events. You should not write code that relies upon state
order."
Now, I'm confused what to do.
Thank you and best wishes for holidays.
}
I'm writing small c# media player app. Situation is like this:
I want it to read playlist from file (works great).
I want it to remove song once that song is finished playing (I have
problems with this one).
My code is here:
private void Form1_Load(object sender, EventArgs e)
{
axPlayer.PlayStateChange += new
AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(axPlayer_PlayStateChange);
string myPlaylist = "moja_zadnja"; // playlist name
//WMPLib.IWMPPlaylist pl;
WMPLib.IWMPPlaylistArray plItems;
plItems = axPlayer.playlistCollection.getByName(myPlaylist);
//pl = plItems.Item(0);
if (plItems.count == 0)
{
// MessageBox.Show("COUNT = 0");
pl = axPlayer.playlistCollection.newPlaylist(myPlaylist);
}
else
{
//MessageBox.Show("COUNT != 0");
pl = plItems.Item(0);
}
axPlayer.settings.autoStart = true; //not necessary
axPlayer.currentPlaylist = pl; //Set media player to use
the playlist.
axPlayer.Ctlcontrols.play(); // start playing
}
private void axPlayer_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8)//Media stopped
{
// state 8 occurs when going to next song so I used that
IWMPMedia trenutni_medij =
axPlayer.currentPlaylist.Item[0]; // current media
axPlayer.currentPlaylist.removeItem(trenutni_medij);
// problem is that state 8 occurst when removing media, so situation
is like this, it plays first song, then doesn't play second, then plays
third and since then, everything is ok.
}
else if (e.newState == 1)
{
axPlayer.Ctlcontrols.play();
// I put this here because when first song is played, // mediaplayer
stops (goes to state 1)
//downside of this else if is that it replays last song. But when I
remove else if (e.newState == 1) then mediaplayer stops after first song.
}
MSDN here
http://msdn.microsoft.com/en-us/library/windows/desktop/dd562460(v=vs.85).aspx
states:
"Remarks
Windows Media Player states are not guaranteed to occur in any
particular order. Furthermore, not every state necessarily occurs during
a sequence of events. You should not write code that relies upon state
order."
Now, I'm confused what to do.
Thank you and best wishes for holidays.
}