多个URL播放C#Windows Media Player

时间:2015-02-19 10:29:02

标签: c# media television windows-media-player

我一直在尝试在C#表单应用程序中制作电视,到目前为止一切都很顺利,但是我在这里寻找的是如何在当前的URL完成时命令它播放下一个URL

如果它应该是电视,它不应该停止播放!我想要我的程序,当用户选择一个特定的频道时,不断播放一堆网址而不是一个,直到用户切换到另一个频道,然后它开始播放另一组网址。

如果当用户切换回上一个频道而不是在应该播放的URL列表中播放随机新URL时恢复视频,这将是非常棒的。

1 个答案:

答案 0 :(得分:0)

尝试连接PlayStateChange事件处理程序并使用它来重新启动具有下一个URL的播放器。以下是来自我的程序的一些代码片段,它们一遍又一遍地重复相同的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