尝试连接一个 PlayStateChange 事件处理程序并使用它通过下一个 URL 重新启动播放器。这是我的一个程序中的几个sn-ps代码,它一遍又一遍地重复相同的URL,但原理应该是一样的。
// Reference to an Interop object for the COM object that interfaces with Microsoft Windows
// Media Player
private readonly WindowsMediaPlayer _windowsMediaPlayer = null;
// Number of repeats left, negative = keep looping
private int _repeatCount = -1;
...
// Instantiate the Windows Media Player Interop object
_windowsMediaPlayer = new WindowsMediaPlayer();
// Hook up a couple of event handlers
_windowsMediaPlayer.MediaError += WindowsMediaPlayer_MediaError;
_windowsMediaPlayer.PlayStateChange += WindowsMediaPlayer_PlayStateChange;
...
/// <summary>
/// Method to start the media player playing a file.
/// </summary>
/// <param name="fileName">complete file name</param>
/// <param name="repeatCount">zero = repeat indefinitely, else number of times to repeat</param>
[SuppressMessage("Microsoft.Usage", "CA2233:OperationsShouldNotOverflow", MessageId = "repeatCount-1")]
public void PlayMediaFile(string fileName, int repeatCount)
{
if (_windowsMediaPlayer == null)
return;
_repeatCount = --repeatCount; // Zero -> -1, 1 -> zero, etc.
if (_windowsMediaPlayer.playState == WMPPlayState.wmppsPlaying)
_windowsMediaPlayer.controls.stop(); // Probably unnecessary
_windowsMediaPlayer.URL = fileName;
_windowsMediaPlayer.controls.play();
}
...
/// <summary>
/// Event-handler method called by Windows Media Player when the "state" of the media player
/// changes. This is used to repeat the playing of the media for the specified number of
/// times, or maybe for an indeterminate number of times.
/// </summary>
private void WindowsMediaPlayer_PlayStateChange(int newState)
{
if ((WMPPlayState)newState == WMPPlayState.wmppsStopped)
{
if (_repeatCount != 0)
{
_repeatCount--;
_windowsMediaPlayer.controls.play();
}
}
}
不知道这是否也适用于您的应用程序,但也许。
编辑:
只记得我不久前回答了一个类似的问题,并在那里发布了我的整个程序。 https://stackoverflow.com/a/27431791/253938