c# - 'MediaElement.SetSource()' isn't updating consistently -


my program should play video when user presses 'play' button. while this, first time press 'play' button nothing happen.

i have traced bug following code, sets mediaelement 'videoplayer':

public void playvideo_tapped(object sender, tappedroutedeventargs e) {   setupvideo();   videoplayer.play(); }  public async void setupvideo() {   if(vm == null) return;   storagefile videofile = vm.videofile;    if (videofile == null || !videofile.contenttype.equals("video/mp4")) return;      using (irandomaccessstream filestream = await videofile.openasync(fileaccessmode.read))     {       videoplayer.setsource(filestream, videofile.contenttype);     } } 

the culprit seems 'setsource()' method @ end. variable changes first click of 'play' next variable 'videoplayer.playtosource', changed null real value.

(as side note, variable 'videoplayer.currentstate' changes 'closed' 'opening' resets 'closed' before second click. 'playtosource' changes functionality.)

i figured quick-fix doing in first method:

  setupvideo();   setupvideo();   videoplayer.play(); 

not great code ought set things straight, right? nope! causes nullreferenceexception. on second call 'setupvideo()' find 'playtosource' still has value , 'videoplayer.currentstate' still set 'opening'... somehow triggers nullreferenceexception.

i'm expecting solution 1 of following things:

1.) set 'videoplayer.playtosource' on first click before calling 'setsource'.

2.) in quick-fix, set 'videoplayer.currentstate' 'closed' in between calls.

3.) other thing mimics first click doing.

of course, both of ideas involve changing read-only variable. i'm getting stuck. i'll include .xaml code measure, i'm confident it's method 'setsource' that's root of troubles:

<grid x:name="videoviewerparentgrid" background="darkgreen" height="{binding videoviewerparentgridheight }" width="{binding videoviewerparentgridwidth}">     <mediaelement x:name="videoplayer" horizontalalignment="center" verticalalignment="bottom" stretch="uniform"                   visibility="{binding videovisibility, converter={staticresource visibilityconverter}}"/>     <button style="{staticresource backbuttonstyle}" tapped="videoviewerclose_tapped" horizontalalignment="left" verticalalignment="top"/>     <button name="play_button" content="play video" fontsize="26" tapped="playvideo_tapped"             verticalalignment="top" horizontalalignment="left" height="60" width="180" margin="0,80,0,0"/> </grid> 

---- update ----

some more poking has revealed on first click 'videoplayer.currentstate' never reaches 'playing' state, instead going 'opening' right 'closed'. not on subsequent clicks long program running. still investigating cause of this.

you missing "await" keyword. this:-

await setupvideo(); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -