Interaction with a "system-wide" media player

I want to create a music app for Windows 10, and I'm curious about the Groove Music interface next to the volume bar. I tried Google to get more information about this, but I had no success. When I play music in Groove Music, and I raise or lower the volume, the title, as well as the artist and album cover of the current song appear with music controls next to the volume indicator:

screen shot

I was wondering how I can create this dialog box in my own application and with which window API I should have a look.

+7
c # windows windows-10 microsoft-metro media-player
source share
3 answers

You need to use SystemMediaTransportControls

Here is the basic setup with Play and Pause. If you like to include additional controls, you can use the available properties for ex.

systemControls.IsNextEnabled = true;

and you should add case to the button switcher.

 case SystemMediaTransportControlsButton.Next: //handle next song break; 

Xaml

 <MediaElement x:Name="mediaElement" Height="100" Width="100" AreTransportControlsEnabled="True"/> 

FROM#

 public MainPage() { this.InitializeComponent(); systemControls = SystemMediaTransportControls.GetForCurrentView(); // Register to handle the 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(); }); } 

Exit

Output

In addition, if you want all this to work in the background, you will need to make a declaration in Package.appxmanifest for background tasks, turn on the sound and add an entry point, for example TestUWP.MainPage

enter image description here

+5
source share

I am going to add my contribution to this, although there is a big answer already @Stamos , because I found that it is actually possible to use SystemMediaTransportControls from a native Windows application (and not just a universal application).

Firstly, he still needs a link to universal winmd files, so it will only work on Win10. They will be located in 10 sdk, and you can add them through the usual Add Reference -> Browse , but you may need to change the filter in the lower right corner of the dialog box to β€œAll files” to display them. They are here on my pc:

  • Windows.Foundation.UniversalApiContract :
    C: \ Program Files (x86) \ Windows Kits \ 10 \ References \ Windows.Foundation.UniversalApiContract \ 1.0.0.0 \ Windows.Foundation.UniversalApiContract.winmd
  • Windows.Foundation.FoundationContract :
    C: \ Program Files (x86) \ Windows Kits \ 10 \ References \ Windows.Foundation.FoundationContract \ 2.0.0.0 \ Windows.Foundation.FoundationContract.winmd

After you have the necessary links, you will encounter another problem: you cannot access the transport controls through the usual SystemMediaTransportControls.GetForCurrentView(); (it will throw an exception) because in reality you do not have a universal representation. This is facilitated by using the following:

 SystemMediaTransportControls systemControls = BackgroundMediaPlayer.Current.SystemMediaTransportControls; 

After that, feel free to use any of the online samples or Stamos answer.

+3
source share
 <MediaElement x:Name="Media" AreTransportControlsEnabled="True"> <MediaElement.TransportControls> <MediaTransportControls Style="{StaticResource MediaTCStyle}"/> </MediaElement.TransportControls> </MediaElement> 

The style is quite large, so I am attaching the MediaTransportControls styles and templates link. I got the style from the article (link above) and changed it in my own ResourceDictionary.

-one
source share

All Articles