暂停/停止视频流C#

时间:2013-11-08 05:35:00

标签: c# wpf media-player mediaelement

伙计我正在WPF应用程序(C#)中的媒体元素中播放视频文件。

我现在需要设置一个上下文菜单(即右键单击你可以获得播放/暂停/停止视频的选项)。 我做了上面的

 ContextMenu contextMenu1 = new ContextMenu();
            MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
            MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
            MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidPlay.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

并将其作为

的“网格”提供
 //video play pause 
            contextMenu1.Items.Add(vidPlay); contextMenu1.Items.Add(vidPause); contextMenu1.Items.Add(vidStop);

单击这些子程序时的子程序为

 //play pause routine handlers
        //'me' is the media element,s name
        void PlayVid_Click(object sender, EventArgs e)
        {
            me.Play();
        }

        void PauseVid_Click(object sender, EventArgs e)
        {
            me.Pause();
        }

        void StopVid_Click(object sender, EventArgs e)
        {
            me.Stop();
        }

但是,当点击任何上下文菜单项时,视频不会暂停或停止。

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

好的家伙,我想我看到了哪里出错了(感觉很好)

 ContextMenu contextMenu1 = new ContextMenu();
            MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
            MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
            MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidPlay.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

我粘贴它时忘了纠正第2和第3项中的vidPlay:/ 更正:

   ContextMenu contextMenu1 = new ContextMenu();
                MenuItem vidPlay = new MenuItem() { Header = "Play" }; vidPlay.Click += new System.Windows.RoutedEventHandler(PlayVid_Click);
                MenuItem vidPause = new MenuItem() { Header = "Pause" }; vidPause.Click += new System.Windows.RoutedEventHandler(PauseVid_Click);
                MenuItem vidStop = new MenuItem() { Header = "Stop" }; vidStop.Click += new System.Windows.RoutedEventHandler(StopVid_Click);

还有一点额外的提示(这对我来说几个小时都很痛苦),你可能不得不使用这些线

  //setting behaviour of MediaElement-This allows to INVOKE the play() / pause() / stop() methods appropriately *08 nv 2013*
                me.LoadedBehavior = MediaState.Manual;
                me.UnloadedBehavior = MediaState.Stop;

这样您就不会在调用play()pause()stop()方法时遇到麻烦