c# - Playing incoming audio stream on different pages -
c# - Playing incoming audio stream on different pages -
i building shared project connected playing incoming net radio stream. ve got problem playing stream on different pages. made background task , works fine can minimize app when moving different page user read rss feed (frame.navigate datacontext contains rss),the media element stop playing.
i seek find solution myself, didn't find best my.
thank help
public sealed partial class mainpage : page { systemmediatransportcontrols systemcontrols = systemmediatransportcontrols.getforcurrentview(); feeddatasource feeddatasource = (feeddatasource)app.current.resources["feeddatasource"]; feeddata nfeeddata = new feeddata(); public mainpage() { this.initializecomponent(); initializetransportcontrols(); } private void media_currentstatechanged(object sender, routedeventargs e) { switch (media.currentstate) { case mediaelementstate.closed: systemcontrols.playbackstatus = mediaplaybackstatus.closed; break; case mediaelementstate.paused: systemcontrols.playbackstatus = mediaplaybackstatus.paused; break; case mediaelementstate.playing: systemcontrols.playbackstatus = mediaplaybackstatus.playing; break; case mediaelementstate.stopped: systemcontrols.playbackstatus = mediaplaybackstatus.stopped; break; default: break; } } private async void playbutton_click(object sender, routedeventargs e) { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { media.play(); }); } private async void stopbutton_click(object sender, routedeventargs e) { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { media.stop(); }); } private async void pausebutton_click(object sender, routedeventargs e) { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { media.pause(); }); } private void volumeslider_valuechanged(object sender, rangebasevaluechangedeventargs e) { media.volume = e.newvalue; } void initializetransportcontrols() { // windows.media systemcontrols.buttonpressed += systemcontrols_buttonpressed; systemcontrols.isplayenabled = true; systemcontrols.ispauseenabled = true; } // handle buttonpressed systemcontrols void systemcontrols_buttonpressed(systemmediatransportcontrols sender, systemmediatransportcontrolsbuttonpressedeventargs args) { switch (args.button) { case systemmediatransportcontrolsbutton.pause: media.play(); break; case systemmediatransportcontrolsbutton.play: media.pause(); break; default: break; } } protected override async void onnavigatedto(navigationeventargs e) { if (feeddatasource != null) { if (feeddatasource.feeds.count == 0) { await feeddatasource.getfeedsasync(); } menusection.datacontext = feeddatasource.feeds; } } private void itemlistview_selectionchanged(object sender, selectionchangedeventargs e) { } private void contetnview_itemclick(object sender, itemclickeventargs e) { } private void contetnview_selectionchanged(object sender, selectionchangedeventargs e) { var x = ((gridview)sender).selecteditem; var z = ((grid)x).tag.tostring(); switch (z) { case "1": frame.navigate(typeof(readnewspage), feeddatasource.feeds[0]); break; case "2": frame.navigate(typeof(readnewspage), feeddatasource.feeds[3]); break; case "3": frame.navigate(typeof(readnewspage), feeddatasource.feeds[2]); break; case "4": frame.navigate(typeof(readnewspage), feeddatasource.feeds[1]); break; } } } <grid x:name="thisgris" background="{themeresource applicationpagebackgroundthemebrush}"> <mediaelement x:name="media" source="http://5.201.13.191:80/live" horizontalalignment="left" height="206" margin="107,372,0,0" verticalalignment="top" width="213" audiocategory="backgroundcapablemedia" autoplay="false" currentstatechanged="media_currentstatechanged"/> <hub x:name="mainhubpage" header="" margin="0,3,0,-3" > <hub.background> <imagebrush /> </hub.background> <hubsection header="radio" width="402" isheaderinteractive="true"> <datatemplate> <grid width="429" height="565" d:islocked="true"> <button x:name="playbutton" content="play" horizontalalignment="left" margin="118,352,0,0" verticalalignment="top" height="58" width="83" click="playbutton_click" d:islocked="true"/> <button x:name="stopbutton" content="stop" horizontalalignment="left" margin="7,352,0,0" verticalalignment="top" height="58" click="stopbutton_click" d:islocked="true"/> <button x:name="pausebutton" content="pause" horizontalalignment="left" margin="236,352,0,0" verticalalignment="top" height="58" click="pausebutton_click" d:islocked="true"/> <slider x:name="volumeslider" horizontalalignment="left" margin="53,295,0,0" verticalalignment="top" width="267" minimum="0" value="50" maximum="100" valuechanged="volumeslider_valuechanged" d:islocked="true"/> <textblock horizontalalignment="left" margin="10,129,0,0" textwrapping="wrap" fontsize="20" text="na antenie :" verticalalignment="top" height="40" width="135" d:islocked="true"/> <textblock horizontalalignment="left" margin="10,174,0,0" textwrapping="wrap" fontsize="20" text="gramy :" verticalalignment="top" height="42" width="135" d:islocked="true"/> <textblock horizontalalignment="left" margin="10,216,0,0" fontsize="20" textwrapping="wrap" text="wkrótce : " verticalalignment="top" height="42" width="118" d:islocked="true"/> <textblock horizontalalignment="left" margin="150,216,0,0" textwrapping="wrap" text="textblock" verticalalignment="top" height="42" width="269" d:islocked="true"/> <textblock horizontalalignment="left" height="40" margin="150,129,0,0" textwrapping="wrap" text="textblock" verticalalignment="top" width="269" d:islocked="true"/> <textblock horizontalalignment="left" margin="150,174,0,0" textwrapping="wrap" text="textblock" verticalalignment="top" height="42" width="293" d:islocked="true"/> </grid> </datatemplate> </hubsection> <hubsection x:name="menusection" width="606" isheaderinteractive="true"> <datatemplate> <gridview x:name="contetnview" datacontext="{binding}" height="519" width="606" selectionchanged="contetnview_selectionchanged" > <grid x:name="dowiadomosci" height="135" width="161" tag="1" datacontext="{binding }"> <textblock text="wiadomości"/> </grid> <grid x:name="dodupy" height="135" width="161" tag="2" datacontext="{binding path=feeds[0]}" > <textblock text="muzyka"/> </grid> <grid height="135" width="161" tag="3" datacontext="{binding path=feeds[0]}"> <textblock text="kultura"/> </grid> <grid height="135" width="161" tag="4" datacontext="{binding source=feeddatasource, path=feeds[0]}"> <textblock text="sport"/> </grid> <grid height="135" width="161" tag="5"> <textblock text="o nas"/> </grid> <grid height="135" width="161" tag="6"> <textblock text="audycja autorskie"/> </grid> <grid height="135" width="161" tag="7"> <textblock text="ekipa"/> </grid> <grid height="135" width="161" > <textblock text="ramówka"/> </grid> </gridview> </datatemplate> </hubsection> <hubsection x:name="schedule" width="606" header="ramówka" isheaderinteractive="true" datacontext="{binding source=feeddatasource, path=feeds[0]}"> <datatemplate> <gridview x:name="schedulecontent" height="519" width="606" itemclick="contetnview_itemclick"> <grid height="135" width="161" > <textblock text="ramówka"></textblock> </grid> </gridview> </datatemplate> </hubsection> <hubsection x:name="team" width="606" header="ekipa" isheaderinteractive="true" datacontext="{binding source=feeddatasource, path=feeds[0]}"> <datatemplate> <gridview x:name="teamcontent" height="519" width="606" itemclick="contetnview_itemclick"> <gridview.itemtemplate> <datatemplate> <grid height="135" width="161" > <image> </image> </grid> </datatemplate> </gridview.itemtemplate> </gridview> </datatemplate> </hubsection> <hubsection x:name="authorshow" width="606" header="audycje autorskie" isheaderinteractive="true" datacontext="{binding source=feeddatasource, path=feeds[0]}"> <datatemplate> <gridview x:name="authorshowcontent" height="519" width="606" itemclick="contetnview_itemclick"> <grid height="135" width="161" > <image> </image> </grid> </gridview> </datatemplate> </hubsection> <hubsection x:name="aboutus" width="606" header="o nas" isheaderinteractive="true" datacontext="{binding source=feeddatasource, path=feeds[0]}"> <datatemplate> <grid x:name="aboutuscontent" height="519" width="606" > <textblock text="tutajonas"></textblock> </grid> </datatemplate> </hubsection> </hub> </grid>
it seems reason music stops because creating stream in page, , moving away page causing allocated go away.
i recommend trying create/play media player object in app.xaml.cs file. should live across page calls.
c# xaml windows-phone-8 windows-8.1
Comments
Post a Comment