HTMLMediaElement, Media Source Extension, and Encrypted Media Extension methods.
Get started
Setup
-
Add the following library dependency to the
dependenciessection of your package.json file. -
Open a terminal window and navigate to your app folder. Run npm to install the package in your app,
-
Update your babel.config.js file, otherwise the app throws a “ReferenceError: Property ‘React’ doesn’t exist” exception during runtime.
-
Add the following privileges in your manifest.toml.
Usage
Video react native component
Video is a React Native component that implements theHTMLVideoElement interface that extends the HTMLMediaElement interface.
Use the component to play audio and video content in formats such as MP4, MPEG DASH, and HLS containing both audio and video.
The component supports the default media controls GUI.
Add the component to the render tree and store its reference. Then call the HTMLMediaElement and HTMLVideoElement methods on the reference. The component doesn’t allow apps to prebuffer content before displaying the video on the screen. For prebuffering, you can use the VideoPlayer Javascript class component.
VideoPlayer component
TheVideoPlayer component implements the HTMLVideoElement interface that extends the HTMLMediaElement interface.
HTMLVideoElement is a typescript class and not a React Native component.
It doesn’t render video to the screen by default, nor does it render the media controls GUI.
Apps must build their own media controls UI and control the playback experience.
Apps must create an instance of VideoPlayer and use it to start buffering the content without rendering video on screen.
To show the video on screen, the app must do the following:
- Add the
KeplerVideoSurfaceViewReact Native component to the render tree - Receive
onSurfaceViewCreatedevent - Pass the surface handle obtained in the event callback to
VideoPlayerthroughsetSurfaceHandlemethod
Audio react native component
Audio is a React Native component that implements theHTMLAudioElement interface that extends the HTMLMediaElement interface.
Use the component to play audio such as MP3 or MPEG DASH/HLS that contain audio only.
The component supports the default media controls GUI.
Add the component to the render tree and store its reference.
Then call the HTMLMediaElement and HTMLAudioElement APIs on this reference.
The component doesn’t allow apps to prebuffer content before playing audio on the device. For prebuffering, use the AudioPlayer Javascript class component.
AudioPlayer component
TheAudioPlayer component implements the HTMLAudioElement interface that extends the HTMLMediaElement interface.
AudioPlayer is a typescript class and not a React Native component.
Use the component to play formats such as MP3 or MPEG DASH/HLS that contain audio only.
Apps need to create an instance of AudioPlayer and use it to start buffering the content without starting playback.
It does not render the media controls GUI. Apps are expected to build their own media controls UI and control the playback experience.
KeplerVideoSurfaceView
KeplerVideoSurfaceView is a React Native component that renders video frames on the screen.
Use KeplerVideoSurfaceView only when using VideoPlayer in prebuffering mode and more than one player simultaneously prebuffers different content.
The app then attaches the surface handle passed by the onSurfaceViewCreated event callback to the video player instance that needs to be rendered on screen.
KeplerCaptionsView
KeplerCaptionsView a React Native component that renders closed captions and subtitles on the screen. Use the component only when operating in prebuffering mode using VideoPlayer and AudioPlayer components.
Apps attach the captions handle passed by the onCaptionViewCreated event callback to VideoPlayer and AudioPlayer through the setCaptionViewHandle method so that the player is able to render captions or subtitles on screen.
Currently, the captions are not rendered by default.
To enable rendering, run the following command:
HTMLMediaElement
HTMLMediaElement is the main player interface that your React Native for Vega apps uses to control the playback. The Video and Audio React Native components, as well as the VideoPlayer and AudioPlayer typescript components implement this interface.
For more details, see https://html.spec.whatwg.org/multipage/media.html.
The app can initiate the Playback in two modes depending on the type of content (Adaptive or non-adaptive) as described below.
-
URL Mode playback for non-adaptive streaming formats such as MP4, MP3, MKV etc media files.
Audio and Video components support playback of non-adaptive streaming formats like MP4, MP3, MKV etc through “src” attribute.
-
Set the src attribute to a media URL.
Apps set the content URL to the “src” attribute.
video.src = [some url] or audio.src = [some url]
-
Set the src attribute to a media URL.
Apps set the content URL to the “src” attribute.
-
Media Source Extension (MSE) mode for playback of adaptive streaming formats such as HLS and MPEG DASH.
The MSE APIs support daptive streaming formats such as HLS and MPEG DASH.
This API allows apps to inject the media segments of adaptive streaming contents such as HLS and DASH to the player. Apps download the manifest and parse it to determine the different bitrate quality levels and variants that the content supports. The app downloads a media segment of an appropriat quality level based on the available network bandwidth. It then passes it to the player through
MediaSourceandSourceBuffermethods. For additional information about how to use these APIs, see https://www.w3.org/TR/media-source-2/.-
Create a MediaSource object.
let mediaSource = new MediaSource(); -
Create a blob URL for MediaSource:
let url = URL.createObjectURL(mediaSource) -
Attach MediaSource to video or audio component.
video.src = urloraudio.src = url -
Add a
SourceBufferby mime type.
let sourceBuffer = mediaSource.addSourceBuffer(mimeType) -
Download and append a media segment to
SourceBuffer:
-
Create a MediaSource object.
Common Media operations
Use the Audio or Video React Native component references to control the media playback and other common operations through theHTMLMediaElement methods as follows.
Start playback
video.play() or audio.play()
Pause playback
video.pause() or audio.pause()
Get current time
Read thecurrentTime attribute to get the current playback position.
console.log('${video.currentTime}'); or console.log('${audio.currentTime}');
Seek to a position
Set thecurrentTime attribute to seek to the required position.
video.currentTime = seekPosition; or audio.currentTime = seekPosition;
Handling Events
The Vega W3C Media API supports all the events as described in the W3C media specifications. There are two ways to register an event handler on the Video or Audio.- Use the props on the Audio/Video React Native component.
Refer to
MediaPropsfor the list of supported props. For example, you can register an event handler forendedas shown the following example.
-
Use the
EventTarget addEventListener()method. The Video and Audio component implements two methods ofEventTargetinterface:addEventListenerandremoveEventListener. Apps can use these methods to register event handlers on the reference of Audio/Video React Native components. For example, you can register the event handler as shown in the following example.video.addEventListener("ended", onEnded);
AudioTrackList, VideoTrackList, MediaSource, SourceBuffer, and SourceBufferList that also emit events. These events are handled through the addEventListener method of EventTarget. All these methods support addEventListener.
Note: The EventHandler attributes of these methods are not supported.
For example, The following exmamples show how to handle the addtrack event on VideoTrackList.
video.videoTracks.addEventListener("addtrack", onVideoTrackAdded);
Or to handle the sourceopen event on MediaSource.
mediasource.addEventListener("sourceopen", onMediaSourceOpen)
Encrypted Media Extension to supported DRM protected playback in MSE mode only.
For additional information, see https://w3c.github.io/encrypted-media/.
Unsupported features in HTMLMediaElement
- networkState
- load
- canPlayType - Indicates it supports any type.
- fastSeek - Performs the same seek as setting
currentTime. - getStartDate
- addTextTrack
- preload
- defaultPlaybackRate, playbackRate
- preservesPitch
- loop
- volume, muted
Unsupported features in Media Source Extension (MSE)
MediaSource API (https://www.w3.org/TR/media-source-2/#mediasource)
- The
EventHandlerattributes are not supported. Apps can use theaddEventListenermethod of theEventTargetinterface thatMediaSourceimplements to register the event handler.
audioTracks,videoTracksandtextTracks
SourceBuffer API (https://www.w3.org/TR/media-source-2/#sourcebuffer)
changeTypeEventHandler- The following attributes. For now apps can useaddEventListenermethod of theEventTargetinterface thatSourceBufferimplements to register the event handler.
SourceBufferList API (https://www.w3.org/TR/media-source-2/#sourcebufferlist)
- The following
EventHandlerattributes are not supported. For now apps can useaddEventListenermethod of theEventTargetinterface thatSourceBufferListimplements.
Related topics
Modules
- Audio
- AudioPlayer
- EncryptedMediaInterface
- EventTargetInterface
- HTMLMediaElement
- index
- KeplerCaptionsView
- KeplerMediaControlHandler
- KeplerShareableSurfaceView
- KeplerVideoSurfaceView
- KeplerVideoView
- MediaCapabilities
- MediaCapabilitiesKeySystemConfiguration
- MediaProps
- MediaSource
- MediaSourceInterface
- MediaTypes
- Utils
- Video
- VideoPlayer

