--- title: MediaSession slug: Web/API/MediaSession tags: - API - Media Session API - MediaSession - 媒体 - 引用 - 接口 - 视频 - 音频 translation_of: Web/API/MediaSession ---
{{SeeCompatTable}}{{APIRef("Media Session API")}}
Media Session API 的 MediaSession
接口允许页面为标准媒体交互提供自定义行为.
"none"
, "paused"
, "playing"
.下面的例子创建了一个新的media session,并且给其绑定了一些动作句柄:
if ('mediaSession' in navigator){ navigator.mediaSession.metadata = new MediaMetadata({ title: "Podcast Episode Title", artist: "Podcast Host", album: "Podcast Name", artwork: [{src: "podcast.jpg"}] }); navigator.mediaSession.setActionHandler('play', function() {}); navigator.mediaSession.setActionHandler('pause', function() {}); navigator.mediaSession.setActionHandler('seekbackward', function() {}); navigator.mediaSession.setActionHandler('seekforward', function() {}); navigator.mediaSession.setActionHandler('previoustrack', function() {}); navigator.mediaSession.setActionHandler('nexttrack', function() {}); }
下面例子为暂停和播放设置了时间句柄:
var audio = document.querySelector("#player"); audio.src = "song.mp3"; navigator.mediaSession.setActionHandler('play', play); navigator.mediaSession.setActionHandler('pause', pause); function play() { audio.play(); navigator.mediaSession.playbackState = "playing"; } function pause() { audio.pause(); navigator.mediaSession.playbackState = "Paused"; }
Specification | Status | Comment |
---|---|---|
{{SpecName('Media Session','#the-mediasession-interface','MediaSession')}} | {{Spec2('Media Session')}} | Initial definition. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatChrome(57)}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} |