c# - MediaElement freezes upon launch of video -


i'm working on project designed play both audio , video files through wpf window through mediaelement. xaml window:

<window x:class="homesystem_csharp.mediawindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mediawindow" minheight="480" minwidth="720" windowstyle="none" resizemode="noresize" visibility="visible" cursor="none">     <grid background="black">         <mediaelement loadedbehavior="manual" horizontalalignment="stretch" name="video" verticalalignment="stretch" cursor="none" minheight="480" minwidth="720"/>     </grid> </window> 

this creates window no borders, plan on full-screening in future. though, want more room on desktop. here code controlling mediaelement:

private bool playing = false;          public mediawindow(string dir)         {             initializecomponent();              video.source = new uri(dir);             play();         }          public void play()         {             if (playing)                 return;              if (!this.isvisible)                 this.show();              video.play();              playing = true;         } 

this mediawindow created outside of object, simple mediawindow mw = new mediawindow("c:\\test.mp4");

no matter how i've moved stuff around in code, upon launch every time gui unresponsive, sound plays. can hear video in background, there broken window on screen. black box.

the biggest issue other day working fine, , broke, , have no clue happened. i'm kinda new c#, dont know ton what's going on, i've worked java several years i'm not totally new. can point out i'm doing wrong? can provide other details think got necessary answer. thank help, has been bothering me day no fix!

edit: turns out, if use

public void play()         {             if (playing)                 return;              //if (!this.isvisible)             //    this.show();              video.play();             new application().run(this);             playing = true;         } 

instead, run gui. however, hangs console. fixed hang using this.show(), that's not working. know moving whole project wpf project fix this, i'm trying not other reasons. win32 now. ideas why happening , how fix it? have [stathread] on main function if makes difference.

edit 2: video file i'm playing movie length, , runs in other software prevent being issue development. mediawindow creation. did made win32 console project , set user commands there. made new wpf project, , created xaml gui window. took code files, , copied them win32 project, , call launch in main method mediawindow mw = new mediawindow("c:\\test.mp4"); did way because i'm trying keep away using pure wpf application, , because i'm kinda new c# wasnt sure how create window wanted without copy paste method.

no matter how i've moved stuff around in code, upon launch every time gui unresponsive, sound plays.

i've managed reproduce this. 1 important thing missing in description exact way create , show window in main() method. example, following freezes video leaving sound playing:

[stathread] static void main(string[] args) {     var w = new mediawindow();     w.show();     console.readline(); } 

the next 1 "freezes" console until close window:

[stathread] static void main(string[] args) {     var w = new mediawindow();     w.showdialog();     console.readline(); } 

and gives both working:

static void main(string[] args) {     var thread = new thread(showmediawindow);     thread.setapartmentstate(apartmentstate.sta);     thread.start();      while (true) // test console working     {         console.write("\r" + datetime.now);         thread.sleep(100);     } }  static void showmediawindow() {     new mediawindow().showdialog(); } 

as can see, console , wpf window can't work in single thread.

the window class simple this, way (the xaml same yours):

public partial class mediawindow : window {     public mediawindow()     {         initializecomponent();         video.source = new uri(@"c:\video\test.asf");         play();     }      public void play()     {         video.play();     } } 

i guess that'll showing video player window console.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -