Disable media control keys with javascript

前端 未结 1 1849
太阳男子
太阳男子 2021-01-21 13:08

I\'m building a keyboard-like application on a website using javascript (no jQuery), but instead of one note being played a sound file plays/pauses every time you click on an im

相关标签:
1条回答
  • 2021-01-21 13:42

    The Media Session API lets you listen for media key events and execute arbitrary code on said events:

    navigator.mediaSession.setActionHandler('play', function() { /* Code excerpted. */ });
    navigator.mediaSession.setActionHandler('pause', function() { /* Code excerpted. */ });
    navigator.mediaSession.setActionHandler('seekbackward', function() { /* Code excerpted. */ });
    navigator.mediaSession.setActionHandler('seekforward', function() { /* Code excerpted. */ });
    navigator.mediaSession.setActionHandler('previoustrack', function() { /* Code excerpted. */ });
    navigator.mediaSession.setActionHandler('nexttrack', function() { /* Code excerpted. */ });
    

    To prevent media key controls from fiddling with your elements leave the code block empty as such:

    navigator.mediaSession.setActionHandler('play', function() {});
    
    0 讨论(0)
提交回复
热议问题