i have wpf application uses mediaelement
. want loop part of video. have starttime
, endtime
variables looping purpose. tried dispatchertimer
looping purpose slow loop of less 1 second. tried simple while loop shown below.
while (true) { if(!buttonpressed) { while (meplayer.position < loop.endtime) ; //go till end of loop meplayer.position = loop.starttime; //after reaching end of loop start again } }
with above code, 0.5 second video loops problem above approach once entered infinite while loop can not detect button press hence unable stop loop. question how detect button press in while loop can stop looping video , continue doing other task?
i'm surprised can't information video playback progress. can go around combining task
, dispatcher
(because cross-threading) without blocking ui, below.
mainwindow.xaml.cs
public partial class mainwindow : window, inotifypropertychanged { public event propertychangedeventhandler propertychanged; private cancellationtokensource _tokensource; private double _start; private double _stop; public mainwindow() { initializecomponent(); datacontext = this; media.loadedbehavior = mediastate.manual; start = 0; media.mediaopened += (sender, args) => { stop = (int)media.naturalduration.timespan.totalseconds; setendposition(); }; media.source = new uri("video.wmv", urikind.relative); media.play(); } private async void setendposition() { _tokensource?.cancel(); _tokensource = new cancellationtokensource(); var token = _tokensource.token; await task.run(() => { if (token.iscancellationrequested) return; while (!token.iscancellationrequested) { dispatcher.invoke(() => { double position = media.position.totalseconds; if (position >= stop) setstartposition(); }); thread.sleep(100); } }, token); } private void setstartposition() { media.position = timespan.fromseconds(start); } public double start { { return _start; } set { _start = value; onpropertychanged(); setstartposition(); } } public double stop { { return _stop; } set { _stop = value; onpropertychanged(); } } [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
mainwindow.xaml
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" title="mainwindow" height="422" width="450"> <grid margin="10"> <grid.rowdefinitions> <rowdefinition height="340" /> <rowdefinition height="*" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition /> <columndefinition /> </grid.columndefinitions> <mediaelement grid.row="0" grid.columnspan="2" margin="0,0,0,10" x:name="media" /> <textbox grid.row="1" grid.column="0" text="{binding start}"></textbox> <textbox grid.row="1" grid.column="1" text="{binding stop}"></textbox> </grid>
Comments
Post a Comment