wpf 如何确定 MediaElement 是否正在播放?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4338951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:22:26  来源:igfitidea点击:

How do I determine if MediaElement is playing?

wpfmediaelement

提问by Brian

Seems simple enough, but I cannot figure out any way to determine what the state of a MediaElement is. There are various properties for some states (such as IsBuffering) but I can't find any for states such as Play, Pause, etc. Silverlight seems to have a CurrentState property that shows all these.

看起来很简单,但我想不出任何方法来确定 MediaElement 的状态。某些状态(例如 IsBuffering)有各种属性,但我找不到播放、暂停等状态的任何属性。 Silverlight 似乎有一个 CurrentState 属性可以显示所有这些。

Currently the way I'm determining whether a video is supposed to be playing is watching for various events and a timer that checks to see if any progress is being made.

目前,我确定视频是否应该播放的方式是观察各种事件和一个检查是否有任何进展的计时器。

I'm new to MediaElement and WPF (I'm actually only using MediaElement in a WinForms app). Is there something I am missing?

我是 MediaElement 和 WPF 的新手(我实际上只在 WinForms 应用程序中使用 MediaElement)。有什么我想念的吗?

回答by PeterL

You aren't missing anything. You pretty much have to manually keep track of whether or not the media is playing. It's a pity, since it is so easy in Silverlight, as you mention. Seems like a major oversight to me.

你没有错过任何东西。您几乎必须手动跟踪媒体是否正在播放。很遗憾,因为正如您所提到的,在 Silverlight 中这很容易。对我来说似乎是一个重大疏忽。

回答by Rich S

You can get at the _currentStatemember using reflection.

您可以_currentState使用反射获取成员。

    private MediaState GetMediaState(MediaElement myMedia)
    {
        FieldInfo hlp = typeof(MediaElement).GetField("_helper", BindingFlags.NonPublic | BindingFlags.Instance);
        object helperObject = hlp.GetValue(myMedia);
        FieldInfo stateField = helperObject.GetType().GetField("_currentState", BindingFlags.NonPublic | BindingFlags.Instance);
        MediaState state = (MediaState)stateField.GetValue(helperObject);
        return state;
    }

This covers play/pause, but doesn't change from 'play' to 'stop' when it finishes.

这包括播放/暂停,但在完成时不会从“播放”变为“停止”。

You can get around this by adding an event handler to the MediaEndedevent, and running the .Stop()method, this changes the status correctly (which can be picked up by the method above)

您可以通过向事件添加事件处理程序MediaEnded并运行该.Stop()方法来解决此问题,这会正确更改状态(可以通过上述方法获取)

回答by Developer

What I did to "work around" that was subclass MediaPlayer (this would work for MediaElement as well) and add my own methods to Play/Pause/Stop. In those methods, I maintain a field which represents the playback status. Also, you need to hook MediaEnded so that you can change the status from 'playing' to 'stopped.'

我为“解决”子类 MediaPlayer 所做的工作(这也适用于 MediaElement)并将我自己的方法添加到播放/暂停/停止。在这些方法中,我维护了一个代表播放状态的字段。此外,您需要挂钩 MediaEnded,以便您可以将状态从“正在播放”更改为“已停止”。

回答by Choletski

For Universal Windows 10 Platform (UWP):

对于Universal Windows 10 Platform (UWP)

if ( yourMediaElement.CurrentState.ToString() == "Playing" ) 
{
    //nou yourMediaElement is playng
}

or below if you wana use an enuminstead:

或者下面,如果你想使用一个enum代替:

if (yourMediaElement.CurrentState == MediaElementState.Playing)
{
    //nou yourMediaElement is playng
}

回答by gian rondanelli

And based on Rich S an extension can be implement

并且基于 Rich S 可以实现扩展

//don't forget
using System.Windows.Controls;
using System.Reflection;


 public static class Util
    {
     public static MediaState GetMediaState(this MediaElement myMedia)
        {
            FieldInfo hlp = typeof(MediaElement).GetField("_helper", BindingFlags.NonPublic | BindingFlags.Instance);
            object helperObject = hlp.GetValue(myMedia);
            FieldInfo stateField = helperObject.GetType().GetField("_currentState", BindingFlags.NonPublic | BindingFlags.Instance);
            MediaState state = (MediaState)stateField.GetValue(helperObject);
            return state;
        }
    }

回答by user3714810

For Silverlightuse CurrentState to check whether the state is playing or pause

用于Silverlight使用 CurrentState 检查状态是正在播放还是暂停

 if (YourMediaElementName.CurrentState == MediaElementState.Playing)
 {
     // TODO: when media is playing.
 }

回答by Alex Pigida

For the WPFMediaElement, here is the solution/workaround I use:

对于WPFMediaElement,这是我使用的解决方案/解决方法:

bool IsPlaying()
{
    var pos1 = wpfMediaElement.Position;
    System.Threading.Thread.Sleep(1);
    var pos2 = wpfMediaElement.Position;

    return pos2 != pos1;
}

回答by Сергей Червонос

For WPF use this: var playing = player.Position < player.NaturalDuration;

对于 WPF 使用这个: var playing = player.Position < player.NaturalDuration;