简体   繁体   中英

How to handle the Keyboard music shortcuts (play/pause/previous/next) in UWP?

I'm developing a Music player app for UWP which can play music in background. Note that I'm using old 2 layer music app pattern one for foreground and one for background music.

In the app i want to handle the Keyboard music controls like Play, Pause, Next and Previous. can anyone help me out with proper way of implementing the keyboard music controls?

The keyboard shortcuts such as Play , Pause and so on you mean here are actually the System Media Transport Controls(SMTC).

can anyone help me out with proper way of implementing the keyboard music controls?

Actually, starting with Windows 10, version 1607, UWP apps that use the MediaPlayer class to play media are automatically integrated with the SMTC by default. Simply instantiate a new instance of MediaPlayer and assign a MediaSource , MediaPlaybackItem , or MediaPlaybackList to the player's Source property and the user will see your app name in the SMTC and can play, pause, and move through your playback lists by using the SMTC controls.

This is the recommended way of interacting with the SMTC for most scenarios.Details please reference Integrate with the System Media Transport Controls . There are a few scenarios where you may need to implement manual control of the SMTC. For this please reference Manual control of the System Media Transport Controls .

but i need it to work even in background.

According to the remark section of SystemMediaTransportControls class:

The system transport control allows a user to control a music application that is in the background as well as get and set the current information on which track is playing.

So that SMTC should also work for music in background.

For a sample please reference the official sample .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM