Skip to content

MediaController

Classes

MediaController

Typedefs

MediaProperties : Object
PlaybackInfo : Object
TimelineProperties : Object
AudioStream : Object

MediaController

Kind: global class

new MediaController()

Handle and control events related to system media and playback.

mediaController.on(eventName, callback)

Add a listener to changes in media events.

Kind: instance method of MediaController

Param Type Description
eventName 'mediaChange' | 'playbackChange' | 'timelineChange' | 'audioStream' - Events with type mediaChange are fired when the current playing media changes (ex. skipping to the next song). - Events with type playbackChange are fired when the media playback state changes (ex. pausing/playing a song, enabling shuffle, etc.). - Events with type timelineChange are fired whenever the timestamp of the current playing media changes (ex. seeking ahead or back, song progressing, etc.). - Subscribing to the audioStream event returns chunks of PCM (pulse-code modulation) samples of the current audio every 100ms (useful for audio visualizations).
callback function Callback receives an object containing one of the following: - mediaChange: MediaProperties - playbackChange: PlaybackInfo - timelineChange: TimelineProperties - audioStream: AudioStream

Example

mediaController.on('mediaChange', (mediaProps) => {
     console.log('Currently playing: ' + mediaProps.title);
});

mediaController.on('playbackChange', (playbackInfo) => {
     if (playbackInfo.playbackStatus == 'Paused')
         console.log('Media is paused.');
});

mediaController.once(eventName, callback)

Add a one-time event listener that removes itself after firing.

Kind: instance method of MediaController

Param Type
eventName 'mediaChange' | 'playbackChange' | 'timelineChange' | 'audioStream'
callback function

mediaController.off(eventName, callback)

Remove an event listener.

Kind: instance method of MediaController

Param Type
eventName 'mediaChange' | 'playbackChange' | 'timelineChange' | 'audioStream'
callback function

mediaController.requestMediaProperties() ⇒ Promise.<MediaProperties>

Request all media properties.

Kind: instance method of MediaController
Returns: Promise.<MediaProperties> - Resolves to a MediaProperties object.
Example

mediaController.requestMediaProperties().then((mediaProps) => {
     console.log('Currently playing: ' + mediaProps.title);
})

mediaController.requestPlaybackInfo() ⇒ Promise.<PlaybackInfo>

Request all playback info.

Kind: instance method of MediaController
Returns: Promise.<PlaybackInfo> - Resolves to a PlaybackInfo object.

mediaController.requestTimelineProperties() ⇒ Promise.<TimelineProperties>

Request all timeline properties.

Kind: instance method of MediaController
Returns: Promise.<TimelineProperties> - Resolves to a TimelineProperties object.

mediaController.requestThumbnail() ⇒ Promise.<string>

Request the thumbnail image of the current playing media.

Kind: instance method of MediaController
Returns: Promise.<string> - Resolves to a base64-encoded image string.
Example

myImage = document.getElementById('img');
mediaController.requestThumbnail().then((thumbnail) => {
     myImage.src = thumbnail;
     console.log('Received thumbnail image');
});

mediaController.play()

Play the current media.

Kind: instance method of MediaController

mediaController.pause()

Pause the current media.

Kind: instance method of MediaController

mediaController.stop()

Stop the current media.

Kind: instance method of MediaController

mediaController.togglePlayPause()

Toggle play/pause. If the current playback state is paused, this will play the current media, and vice versa.

Kind: instance method of MediaController

mediaController.skipNext()

Skip to the next media.

Kind: instance method of MediaController

mediaController.skipPrevious()

Skip to the previous media.

Kind: instance method of MediaController

mediaController.setShuffle(state)

Set the shuffle state.

Kind: instance method of MediaController

Param Type Default Description
state boolean true The shuffle state. Set to true to enable shuffling, false to disable it.

mediaController.setRepeatMode(mode)

Set the repeat mode.

Kind: instance method of MediaController

Param Type Description
mode 'track' | 'list' | 'none' The repeat mode. track enables repeat for the current track, list enables repeat for the current media's playlist/album, none disables repeat.

mediaController.setSeekPosition(position)

Set the seek position.

Kind: instance method of MediaController

Param Type Description
position number The seek position of playback in the current media in seconds.

MediaProperties : Object

Kind: global typedef
Properties

Name Type Description
title string The title of the current media.
albumArtist string The album artist of the current media.
albumTitle string The album title of the current media.
albumTrackCount number The number of tracks in the album of the current media.
artist string The name of the artist of the current media.
genres Array.<string> A list of the current media's genres.
trackNumber number The track number of the current media.

PlaybackInfo : Object

Kind: global typedef
Properties

Name Type Description
playbackStatus 'Closed' | 'Opened' | 'Changing' | 'Stopped' | 'Playing' | 'Paused' | 'Unknown' The status of the current media's playback.
playbackRate number The playback rate of the current media (ex. 1.5 for 1.5x speed-up playback).
shuffleActive boolean Whether or not the current media has shuffle enabled.
playbackType 'Music' | 'Image' | 'Video' | 'Unknown' The type of the current media.

TimelineProperties : Object

Kind: global typedef
Properties

Name Type Description
startTime number The starting time of the current media in seconds.
endTime number The ending time of the current media in seconds.
position number The seek position of playback in the current media in seconds (ex. a user is N seconds into a song).
minSeekTime number The minimum seek time of playback in the current media in seconds.
maxSeekTime number The maximum seek time of playback in the current media in seconds (ex. the length of the media is maxSeekTime - minSeekTime).

AudioStream : Object

Kind: global typedef
Properties

Name Type Description
channels number Number of audio channels (e.g., 2 for stereo).
sampleCount number Number of PCM frames in this chunk.
samples Array.<number> Normalized PCM samples, in interleaved order if stereo.