Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys.
I need to know how can I determine my video is now playing, or in Pause state, or in Stop state.
In WPF.
Any help please?

What I have tried:

I couldn't find any try! Just I used MediaElement from Toolbox.
Posted
Updated 4-Jan-24 12:33pm
Comments
Jo_vb.net 4-Jan-24 18:40pm    
Take a look here:
https://stackoverflow.com/questions/4338951/how-do-i-determine-if-mediaelement-is-playing
Sh.H. 5-Jan-24 4:00am    
@Jo_vb.net
Actually I read that post before. But CurrentState and MediaState doesn't work for me, and I don't know why! The Visual Studio gets exception on this and says that CurrentState is not a member of MediaElement.
[no name] 4-Jan-24 18:45pm    
You infer: if .CanPause, is running; etc.

A quick Google search: WPF MediaElement is playing? - Google Search [^]
found this: How to: Control a MediaElement (Play, Pause, Stop, Volume, and Speed) - WPF .NET Framework | Microsoft Learn[^]

If you read the sample code you will see this:
XML
<MediaElement Source="media\numbers.wmv" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>

MediaElement.MediaEnded Event[^] will be triggered when the media stops playing.

UPDATE

Your question is when paused and when stopped. Above I show you how to identify when it ends playing.

If you are telling the MediaElement to pause or stop manually, then you already know the state of the control, you just need to track the manual state.
 
Share this answer
 
v3
Comments
Sh.H. 5-Jan-24 7:44am    
@Graeme_Grant
Thanks.
But it doesn't triggered on pause.
This is the problem.
Graeme_Grant 5-Jan-24 8:34am    
updated.
Sh.H. 5-Jan-24 10:13am    
@Graeme_Grant
Thanks for update.
Actually I need to determine the state of VideoElement. I mean is it stop? Or Playing? Or Pause?
Jo_vb.net 5-Jan-24 11:41am    
Please show us the C# or VB.Net code you are using to control the VideoElement.
Sh.H. 5-Jan-24 12:29pm    
@Jo_vb.net
I don't have any code!
I am asking for the code of how can I determine the state of VideoElement which is Palying or stop or pause.
You can search @GitHub...

This project
GitHub - Alphabet85/WPFVideoPlayer: A video player created in WPF using MVVM techniques.[^]
uses MVVM concept and it uses properties in VideoPlayerViewModel.cs and ICommands like
private ICommand _pauseCommand;
public ICommand PauseCommand
private ICommand _stopCommand;
public ICommand StopCommand

This may give you another view how you could develop your own project.
 
Share this answer
 
There is no built-in property for determining whether a MediaElement is playing or not.

There are events triggered when a MediaElement is opened or ended. You can use the MediaEnded event to check when the media has finished playing. You can also use the MediaOpened to check when the media loading has finished.

After that the answer to your question depends on the LoadedBehavior property and hence who is controlling the the playback.
 
Share this answer
 
Comments
Sh.H. 6-Jan-24 10:17am    
@Mike-MadBadger
Thanks. So this is same as other answers and comments in this question.
BTW, do you have any suggestion to use another video player in WPF? Like an add-on or any library or etc... ? Because built-in MediaElement of Visual Studio is really poor and has a lot of problems.
Graeme_Grant 6-Jan-24 17:44pm    
Which is basically saying what I have already said.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900