开发者

How do I determine if MediaElement is playing?

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.

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开发者_Python百科 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?


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.


You can get at the _currentState member using reflection.

    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 MediaEnded event, and running the .Stop() method, this changes the status correctly (which can be picked up by the method above)


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.'


For Universal Windows 10 Platform (UWP) :

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

or below if you wana use an enum instead:

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


And based on Rich S an extension can be implement

//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;
        }
    }


For Silverlight use CurrentState to check whether the state is playing or pause

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


For the WPF MediaElement, here is the solution/workaround I use:

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

    return pos2 != pos1;
}


if you use MediaElement.CanPause it will determine if your currently playing media can be paused. Allows you to implement a Play/Pause button

var media = (MediaElement)obj;
  if (media.CanPause)
  {
    media.Pause();
  }
  else
  {
    media.Play();
  }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜