Pri*_*and 1 html google-chrome html5-audio
我正在开发音乐播放器。我成功地能够从网址播放音频并获得播放音乐的通知。
我可以使用播放/暂停按钮来播放/暂停音乐(它会自动工作),但我不知道如何实现下一个和上一个按钮。我正在使用最新版本的 Chrome (84.0.4147.89)。
[编辑1]
最低可重现代码:
<html>
<head>
<title>Xt3m1xMus1c</title>
</head>
<body>
<audio id="song" src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" autoplay></audio>
</body>
<script>
function nextSong() {
// Need to call this function when I press the next button on the notification
document.getElementById("song").setAttribute('src', "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3");
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
这些按钮可以通过Media Session API进行控制。要实现上一个和下一个按钮,您需要设置相应的操作处理程序。
navigator.mediaSession.setActionHandler(
'nexttrack',
() => { /* Your code to play the next track. */ }
);
navigator.mediaSession.setActionHandler(
'previoustrack',
() => { /* Your code to play the previous track. */ }
);
Run Code Online (Sandbox Code Playgroud)
但我认为图中显示的按钮是'seekbackward'和'seekforward'按钮。