MediaSession:playbackState 属性
playbackState
是 MediaSession
接口的一个属性,它指示当前媒体会话是正在播放还是暂停。
值
示例
以下示例设置了用于播放和暂停的两个函数,然后将它们用作回调,并带有相关的操作处理程序。每个函数都利用 playbackState
属性来指示音频是正在播放还是已暂停。
js
const actionHandlers = [
// play
[
"play",
async () => {
// play our audio
await audioEl.play();
// set playback state
navigator.mediaSession.playbackState = "playing";
// update our status element
updateStatus(allMeta[index], "Action: play | Track is playing…");
},
],
[
"pause",
() => {
// pause out audio
audioEl.pause();
// set playback state
navigator.mediaSession.playbackState = "paused";
// update our status element
updateStatus(allMeta[index], "Action: pause | Track has been paused…");
},
],
];
for (const [action, handler] of actionHandlers) {
try {
navigator.mediaSession.setActionHandler(action, handler);
} catch (error) {
console.log(`The media session action "${action}" is not supported yet.`);
}
}
规范
规范 |
---|
媒体会话 # dom-mediasession-playbackstate |
浏览器兼容性
BCD 表格仅在启用 JavaScript 的浏览器中加载。