c# - Global MediaElement will not restart once stopped, wp7 -
i have app runs background music @ application level music doesn't stop when user navigates through pages. however, make use of videobrush. found out, cannot have 2 running @ same time videobrush crash when setting source.
i found if set source of mediaelement null when user attempts use videobrush, works. sure music stops, chagrin, no error happens.
however, when user taps away videobrush, trying make music start (beginning fine) no avail. put, having trouble getting music start again.
here code:
app.xaml
<application.resources> <mediaelement x:key="globalmedia" source="minutelongsong.mp3" mediaended="mediaelement_mediaended" visibility="collapsed" /> </application.resources>
app.xaml.cs
public static mediaelement globalmediaelement { { return current.resources["globalmedia"] mediaelement; } } private void application_launching(object sender, launchingeventargs e) { var appmediaelement = app.globalmediaelement; appmediaelement.position = timespan.zero; appmediaelement.play(); } private void mediaelement_mediaended(object sender, routedeventargs e) { var appmediaelement = app.globalmediaelement; appmediaelement.position = timespan.zero; appmediaelement.play(); }
and page making use of videobrush.
mainpage.xaml
<canvas x:name="viewfindercanvas" width="480" height="800" horizontalalignment="center" verticalalignment="center" visibility="collapsed"> <canvas.background> <videobrush x:name="videobrush" stretch="fill"> <videobrush.relativetransform> <compositetransform x:name="previewtransform" centerx=".5" centery=".5" /> </videobrush.relativetransform> </videobrush> </canvas.background> </canvas>
mainpage.xaml.cs
private void button_mouseleftbuttondown(object sender, mousebuttoneventargs e) { var appmediaelement = app.globalmediaelement; appmediaelement.pause(); appmediaelement.stop(); appmediaelement.source = null; //set null allow cam set. if ((photocamera.iscameratypesupported(cameratype.primary))) { viewfindercanvas.visibility = visibility.visible; cam = new photocamera(cameratype.primary); if (orientation == pageorientation.portraitup || orientation == pageorientation.portraitdown || orientation == pageorientation.portrait) { videobrush.relativetransform = new compositetransform() { centerx = 0.5, centery = 0.5, rotation = 90 }; } videobrush.setsource(cam); }
when user exits out of camera videobrush hitting on screen button, code fired. disposes cam, , tries music playing again if user allows music. however music not play, code.
private void zoomout_mouseleftbuttondown(object sender, mousebuttoneventargs e) { if (cam != null) { cam.dispose(); } viewfindercanvas.visibility = visibility.collapsed; if (allowingamemusic == true) { var appmediaelement = app.current.resources["globalmedia"] mediaelement; appmediaelement.source = new uri("minutelongsong.mp3", urikind.relativeorabsolute); appmediaelement.position = timespan.zero; appmediaelement.play(); //despite here, not play. no error thrown. } }
i switch , forth between capturing image , audio in same page on phone app.
you close, have set videobrush source else or won't let go of resources.
so when want use mediaelement, first dispose of camera this:
cam.dispose(); viewfinderbrush.setsource(new mediaelement()); //so let go of cam cam = null; //now set source mediaelemet , whatever
and when use camera again:
mediaelement.stop(); mediaelement.source = null; //or else setting source video brush fail //now set source videobrush , whatever
old question, someone... took me few tries figure out.
Comments
Post a Comment