File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -217,6 +217,13 @@ export default {
217217 : ' ' ;
218218 },
219219 },
220+ mounted () {
221+ this .setupMediaControls ();
222+ window .addEventListener (' keydown' , this .handleKeydown );
223+ },
224+ beforeDestroy () {
225+ window .removeEventListener (' keydown' , this .handleKeydown );
226+ },
220227 methods: {
221228 ... mapMutations ([' toggleLyrics' ]),
222229 ... mapActions ([' showToast' , ' likeATrack' ]),
@@ -270,6 +277,39 @@ export default {
270277 mute () {
271278 this .player .mute ();
272279 },
280+
281+ setupMediaControls () {
282+ if (' mediaSession' in navigator ) {
283+ navigator .mediaSession .setActionHandler (' play' , () => {
284+ this .playOrPause ();
285+ });
286+ navigator .mediaSession .setActionHandler (' pause' , () => {
287+ this .playOrPause ();
288+ });
289+ navigator .mediaSession .setActionHandler (' previoustrack' , () => {
290+ this .playPrevTrack ();
291+ });
292+ navigator .mediaSession .setActionHandler (' nexttrack' , () => {
293+ this .playNextTrack ();
294+ });
295+ }
296+ },
297+
298+ handleKeydown (event ) {
299+ switch (event .code ) {
300+ case ' MediaPlayPause' :
301+ this .playOrPause ();
302+ break ;
303+ case ' MediaTrackPrevious' :
304+ this .playPrevTrack ();
305+ break ;
306+ case ' MediaTrackNext' :
307+ this .playNextTrack ();
308+ break ;
309+ default :
310+ break ;
311+ }
312+ },
273313 },
274314};
275315< / script>
You can’t perform that action at this time.
0 commit comments