我正在尝试实现无数次循环当前歌曲的代码。
这是我的代码:
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 8) // media ended
{
if (repeat)
{
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0;
//axWindowsMediaPlayer1.Ctlcontrols.previous();
//axWindowsMediaPlayer1.Ctlcontrols.playItem(axWindowsMediaPlayer1.Ctlcontrols.currentItem);
}
}
}
如果我在下一行调试代码,设置currentPosition = 0
工作正常。
但是在调试之后 - 触发了新事件(事件9(转换),然后是事件3(播放)),下一首歌开始播放!
如何防止这种情况发生?
基本上就是现在发生的事情:
编辑:在搞乱我的代码后,我终于设法打破了事件变化链。
public bool ended = false;
public WMPLib.IWMPMedia latest_song;
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (ended)
{
axWindowsMediaPlayer1.Ctlcontrols.playItem(latest_song);
ended = false;
return;
}
if (e.newState == 8) // media ended
{
if (repeat)
{
ended = true;
latest_song = axWindowsMediaPlayer1.Ctlcontrols.currentItem;
}
}
}
我不知道代码是否可以比这更好..
答案 0 :(得分:1)
bool ended = false;
bool skipnext = false;
bool skipnextnext = false;
bool skipextra = false;
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (ended)
{
skipnext = true;
ended = false;
axWindowsMediaPlayer1.Ctlcontrols.playItem(latest_song);
return;
}
if (e.newState == 6) // buffering
{
skipextra = true;
}
if (e.newState == 8) // media ended
{
if (repeat)
{
if (currect_album.Songs.Count < 2) { return; }
ended = true;
latest_song = current_song;
return;
}
}
else if (e.newState == 3) // playing
{
// these "skips" are necessary for scenario: "repeat: on", "click: next"
if (skipnext) { skipnext = false; skipnextnext = true; return; }
if (skipnextnext) { skipnextnext = false; return; }
// Buffering adds 1 more skip! Ask Microfost why ;)
if (skipextra) { skipextra = false; return; }
current_song = axWindowsMediaPlayer1.Ctlcontrols.currentItem;
// your stuff
}
}
P.S。微软程序员 - 如果你看到这篇文章 - 请添加“on_next”和“on_previous”状态!感谢