c# - Interact with "system-wide" media player -
i want develop music app windows 10 , i'm curious interface provided groove music next volume bar. i've tried googling more information haven't had success whatsoever. when i'm playing music in groove music , raise or lower volume, name artist , album artwork of current song show music controls next volume indicator this:
i wondering how create dialog in own app , windows api's i'd have into.
you need use systemmediatransportcontrols
here basic setup play , pause. if enable more controls can using available properties ex.
systemcontrols.isnextenabled = true;
and have add case
in button switch.
case systemmediatransportcontrolsbutton.next: //handle next song break;
xaml
<mediaelement x:name="mediaelement" height="100" width="100" aretransportcontrolsenabled="true"/>
c#
public mainpage() { this.initializecomponent(); systemcontrols = systemmediatransportcontrols.getforcurrentview(); // register handle following system transpot control buttons. systemcontrols.buttonpressed += systemcontrols_buttonpressed; mediaelement.currentstatechanged += mediaelement_currentstatechanged; systemcontrols.isplayenabled = true; systemcontrols.ispauseenabled = true; } private void mediaelement_currentstatechanged(object sender, routedeventargs e) { switch (mediaelement.currentstate) { case mediaelementstate.playing: systemcontrols.playbackstatus = mediaplaybackstatus.playing; break; case mediaelementstate.paused: systemcontrols.playbackstatus = mediaplaybackstatus.paused; break; case mediaelementstate.stopped: systemcontrols.playbackstatus = mediaplaybackstatus.stopped; break; case mediaelementstate.closed: systemcontrols.playbackstatus = mediaplaybackstatus.closed; break; default: break; } } void systemcontrols_buttonpressed(systemmediatransportcontrols sender, systemmediatransportcontrolsbuttonpressedeventargs args) { switch (args.button) { case systemmediatransportcontrolsbutton.play: playmedia(); break; case systemmediatransportcontrolsbutton.pause: pausemedia(); break; case systemmediatransportcontrolsbutton.stop: stopmedia(); break; default: break; } } private async void stopmedia() { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { mediaelement.stop(); }); } async void playmedia() { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { if (mediaelement.currentstate == mediaelementstate.playing) mediaelement.pause(); else mediaelement.play(); }); } async void pausemedia() { await dispatcher.runasync(windows.ui.core.coredispatcherpriority.normal, () => { mediaelement.pause(); }); }
output
also if want work in background have declaration in package.appxmanifest
background tasks, enable audio , add entry point testuwp.mainpage
Comments
Post a Comment