媒体会话 API

The Media Session API provides a way to customize media notifications. It does this by providing metadata for display by the user agent of the media your web app is playing, and allows you to create event handlers, to define your own behaviors for a user-agent playback controls. The aim of this is allowing users to know what's playing, and to control it, without opening the specific page that launched it.

Media Session concepts and usage

MediaMetadata interface lets a web site provide rich metadata to the platform UI for media that is playing. This metadata includes the title, artist (creator) name, album (collection), and artwork. The platform can show this metadata in media centers, notifications, device lockscreens, etc.

MediaSession interface lets users control playback of media through user-agent defined interface elements. Interaction with these elements even trigger handlers in the web page, playing the media. Since multiple pages may be simultaneously using this API, the user agent is responsible for calling the correct page's event handlers. The user agent provides default behaviors, when no page-defined behavior is available.

Accessing the Media Session API

The primary interface for the Media Session API is the MediaSession interface. Rather than creating your own MediaSession instance, you access the API using the navigator.mediaSession property. For example, to set the current state of the media session to playing :

navigator.mediaSession.playbackState = "playing";
					

接口

MediaMetadata
Allows a web page to provide rich media metadata, for display in a platform UI.
MediaSession
Allows a web page to provide custom behaviors, for standard media playback interactions.

字典

MediaImage
A MediaImage object contains information describing an image associated with the media. This might be a CD or DVD cover, a movie poster, a poster frame, or the like.
MediaPositionState
Used to contain information about the current playback position, playback speed, and overall media duration when calling the MediaSession method setPositionState() to establish the media's length, playback position, and playback speed.
MediaSessionActionDetails
Provides information needed in order to perform the action which has been requested, including the type of action to perform and any other information needed, such as seek distances or times.

范例

The following example shows feature detection for the Media Session API. It then instantiates a metadata object for the session, and adds event handlers for the user control actions:

if ('mediaSession' in navigator) {
  navigator.mediaSession.metadata = new MediaMetadata({
    title: 'Unforgettable',
    artist: 'Nat King Cole',
    album: 'The Ultimate Collection (Remastered)',
    artwork: [
      { src: 'https://dummyimage.com/96x96',   sizes: '96x96',   type: 'image/png' },
      { src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
      { src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
      { src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
      { src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
      { src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
    ]
  });
  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. */ });
}
					

Some user agents disable autoplay for media elements on mobile devices and require a user gesture to start media. The following example adds a pointerup event to an on-page play button, which is then used to kick off the media session code:

playButton.addEventListener('pointerup', function(event) {
  var audio = document.querySelector('audio');
  // User interacted with the page. Let's play audio...
  audio.play()
  .then(_ => { /* Set up media session controls, as shown above. */ })
  .catch(error => { console.log(error) });
});
					

规范

规范 状态 Comment
Media Session Standard 草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MediaSession
Chrome 73 Edge ≤79 Firefox 71
Disabled
71
Disabled
Disabled From version 71: this feature is behind the dom.media.mediasession.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0
metadata
Chrome 73 Edge ≤79 Firefox 71
Disabled
71
Disabled
Disabled From version 71: this feature is behind the dom.media.mediasession.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0
playbackState
Chrome 73 Edge ≤79 Firefox No IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0
setActionHandler()
Chrome 73 Edge ≤79 Firefox 71
Disabled
71
Disabled
Disabled From version 71: this feature is behind the dom.media.mediasession.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0
setPositionState()
Chrome 73 Edge ≤79 Firefox No IE No Opera Yes Safari ? WebView Android No Chrome Android 57 Firefox Android No Opera Android No Safari iOS ? Samsung Internet Android 7.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
用户必须明确启用此特征。
用户必须明确启用此特征。