我正在开发一款音乐播放器。我能够成功地从urls播放音频,并获得播放音乐的通知。
我可以使用播放/暂停按钮来播放/暂停音乐(它自动工作),但我不知道如何实现下一步和上一步按钮。我使用的是最新版本的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>
发布于 2020-07-09 04:02:26
这些按钮可以通过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. */ }
);
但我认为图片中显示的按钮是'seekbackward'
和'seekforward'
按钮。
https://stackoverflow.com/questions/62796323
复制相似问题