TAL provides a device-agnostic media playback API via the MediaPlayer
class. This can be used to play video and audio files supported by the device.
The framework only supports the the playback of one item of media at a time. Video can only be played in full screen mode.
You will need to use the correct media player modifier in the device’s configuration file.
Access the media player through the application’s Device
object:
For example, creating a component to playback media:
The MediaPlayer has a number of playback states:
The media playback state changes as API methods are called. Different API methods have different effects depending on the playback state. Transitions to the STOPPED
and ERROR
state have been limited in the state diagram below for clarity. Transitions occur synchronously in response to API methods, except those marked by an asterisk (*) which result from asynchronous behaviour such as buffering or at the end of media.
The playback state can be accessed using getState()
.
EMPTY
initialiseMedia(url)
: store the url and transition to STOPPED
playFrom(), beginPlayback(), beginPlaybackFrom(), pause(), resume() or stop()
: transition to ERROR
STOPPED
MediaPlayer.EVENT.STOPPED
beginPlaybackFrom(time)
: request playback from ‘time’ (clamped to the available range), transition to BUFFERING
reset()
: transition to EMPTY
beginPlayback()
: begin playback from wherever the device can (could be anywhere for Live, usually media start for VOD), transition to BUFFERING
initialiseMedia(), playFrom() pause(), resume() or stop()
: transition to ERROR
STOPPED
BUFFERING
MediaPlayer.EVENT.BUFFERING
playFrom(time)
: seek to time (clamped to the available range), and remember to resume playback and transition to PLAYING
when sufficient data is availablestop()
: transition to STOPPED
pause()
: When buffering is complete, remember to pause playback and transition to PAUSED
when sufficient data is availableresume()
: When buffering is complete, remember to resume playback and transition to PLAYING
when sufficient data is availablebeginPlayback(), beginPlaybackFrom(), reset() or initialiseMedia()
: transition to ERROR
PLAYING
or PAUSED
as requiredMediaPlayer.EVENT.STATUS
PLAYING
MediaPlayer.EVENT.PLAYING
pause()
: pause playback and transition to PAUSEDplayFrom(time)
: seek to time (clamped to the available range) and transition to BUFFERINGstop()
: transition to STOPPED
resume()
: do nothingbeginPlayback(), beginPlaybackFrom(), reset() or initialiseMedia()
: transition to ERROR
MediaPlayer.EVENT.STATUS
COMPLETE
BUFFERING
PAUSED
MediaPlayer.EVENT.PAUSED
resume()
: resume playback from current time and transition to PLAYING
playFrom(time)
: seek to time (clamped to the available range) and transition to BUFFERING
stop()
: transition to STOPPED
pause()
: do nothingbeginPlayback(), beginPlaybackFrom(), reset() or initialiseMedia()
: transition to ERROR
COMPLETE
MediaPlayer.EVENT.COMPLETE
stop()
: transition to STOPPED
playFrom(time)
: seek to time (clamped to the available range) and transition to BUFFERING
beginPlayback(), beginPlaybackFrom(), reset(), initialiseMedia(), pause() or resume()
: transition to ERROR
COMPLETE
ERROR
MediaPlayer.EVENT.ERROR
reset()
: transition to EMPTY
initialiseMedia(), beginPlayback(), beginPlaybackFrom(), pause(), resume(), stop() or playFrom()
: transition to ERROR
ERROR
To initialise the media, the MediaPlayer must be in the EMPTY
state.
You initialise the media by using the initialiseMedia()
method.
It takes a mediaType (MediaPlayer.TYPE.VIDEO
or MediaPlayer.TYPE.AUDIO
), a url, the mimeType of the file and a DOM element to render the media into.
For example, to load a video:
If initialising the media is successful, the playback state will change to STOPPED
and an event will be emitted of type MediaPlayer.EVENT.STOPPED
.
From the STOPPED
state, MediaPlayer
can be used to access the properties of the media source (getSource()
, getMimeType()
, getMimeType()
);
To change the media source, applications must ensure the MediaPlayer is in the STOPPED
state and call reset()
to transition to the EMPTY
state before calling initialiseMedia()
with a different URL.
The beginPlaybackFrom(seconds)
method will attempt to play the media from the provided time (in seconds).
For example, to play media from the start:
This will cause the playback state to change to BUFFERING
and an event emitted of type MediaPlayer.EVENT.BUFFERING
.
beginPlayback()
can also be used. This is sometimes useful when beginPlaybackFrom(seconds)
fails to work because it cannot determine the duration of the media, for example, on a live stream.
When the device has loaded enough of the video to begin playback, the MediaPlayer will transition to the PLAYING
state and an event of type MediaPlayer.EVENT.PLAYING
emitted.
The device may have a slow network connection, meaning the rate of video download is slower than the playback speed.
When this occurs, the MediaPlayer will enter the BUFFERING
state asynchronously and an event of type MediaPlayer.EVENT.BUFFERING
(as above) will be emitted.
By adding an event callback to listen to these events, you can update your application’s interface to, for example, show a buffering spinner.
playFrom(seconds)
can be used to seek to different points in the media. If the seconds parameter is larger than the duration of the media, the value will be clamped and playback will begin from just before the end. Requests to seek within one second of the current time will be ignored to ensure consistent behaviour across devices.
Some devices always composite HTML on top of video playback, so if you use any sort of non-transparent background, playback will not be visible.
If this is the case, you will need to remove the background just before playback, then restore on stop.
One way to do this is via a background-none
CSS class:
To interrupt playback you can call:
The MediaPlayer will transition to the PAUSED
state. From this state you can choose to resume playback:
If you want to close down the video player completely, for example if you are navigating to a different component, you should call
This will transition the MediaPlayer to the STOPPED
state.
MediaPlayer will emit events to notify the application of changes in the playback state.
To listen for these events in your application, use the addEventCallback()
method.
The event
object has the following properties: { type, currentTime, range, url, mimeType, state}
where type
is a string specified in the MediaPlayer.EVENT
enum:
MediaPlayer.EVENT.* | Description |
---|---|
STOPPED | Event fired when playback is stopped |
BUFFERING | Event fired when playback has to suspend due to buffering |
PLAYING | Event fired when starting (or resuming) playing of the media |
PAUSED | Event fired when media playback pauses |
COMPLETE | Event fired when media playback has reached the end of the media |
ERROR | Event fired when an error condition occurs |
STATUS | Event fired regularly during play - use this to update the current playback time |
Some devices do not respond immediately to requests to play, pause or seek through media or do not emit expected events, such as entering buffering. To solve this problem, MediaPlayer will monitor the state of the device to keep it in sync with the API’s state. As a result, the API will fire one of the sentinel events listed below. By listening to for these events in your callback, you can detect deviant media playback behaviour on the device.
MediaPlayer.EVENT.* | Description |
---|---|
SENTINEL_ENTER_BUFFERING | Event fired when a sentinel has to act because the device has started buffering but not reported it |
SENTINEL_EXIT_BUFFERING | Event fired when a sentinel has to act because the device has finished buffering but not reported it |
SENTINEL_PAUSE | Event fired when a sentinel has to act because the device has failed to pause when expected |
SENTINEL_SEEK | Event fired when a sentinel has to act because the device has failed to seek to the correct location |
SENTINEL_COMPLETE | Event fired when a sentinel has to act because the device has completed the media but not reported it |
An example application snippet using media playback events to display the type of event in a label
To get the duration of the media, in seconds, use getDuration()
.
To get the available range in the media that can be seeked in, use getSeekableRange()
, this will return the following object for a non-live video:
getSeekableRange()
is especially useful for live streams, on which the seekable window changes as the live stream progresses.
API errors (e.g. calling pause()
while in the STOPPED
state) are treated as fatal errors and the media player transitions to the ERROR
state and stops all playback. After this, the player must be reset()
.
However, device errors (network errors, playback errors, media incompatibility etc.) are raised as error events in the API, but they do not cause a transition to the error state. This is because many device errors are non fatal, and playback can continue normally afterwards. It is recommended that apps similarly treat these error events as notifications, and do not display modal dialogs or end playback just because of a device error event.