Stream media to a channel
Playing media files during online business presentations, educational sessions, or casual meetups heightens user engagement. Video SDK enables you to add media playing functionality to your app.
This page shows you how to use media player-related APIs to play local or online media resources with remote users in Interactive Live Streaming channels.
Understand the tech
To play a media file in a channel, you open the file using a media player instance. When the file is ready to be played, you set up the local video container to display the media player output. You update channel media options to start publishing the media player stream, and stop publishing the camera and microphone streams. The remote user sees the camera and microphone streams of the media publishing user replaced by media streams.
Prerequisites
Ensure that you have implemented the SDK quickstart in your project.
Implement the logic
To implement a media player in your app, follow these steps:
-
After initializing an
RtcEngine
instance, create anIMediaPlayer
object and register a player observer by calling theregisterPlayerObserver
method. -
Implement the callbacks for the media player observer. Observe the player's state through the
onPlayerStateChanged
callback, get the current media file's playback progress throughonPositionChanged
, and handle player events through theonPlayerEvent
callback. -
Call
setupLocalVideo
to render the local media player view. -
When joining a channel, use
ChannelMediaOptions
to set the media player ID, publish media player audio and video, and share media resources with remote users in the channel. -
Use the
open
method to open a local or online media file. -
Call the
play
method to play the media file.CautionCall the
play
method to play the media file only after receiving theonPlayerStateChanged
callback reporting the player state asPLAYER_STATE_OPEN_COMPLETED
. -
When a user leaves the channel, call
stop
to stop playback,destroy
to destroy the media player,unRegisterPlayerObserver
to unregister the player observer, and release allocated resources.
Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Supported formats and protocols
The media player supports the following media formats and protocols:
Video encoding formats
- H.263, H.264, H.265, MPEG-4, MPEG-2, RMVB, Theora, VP3, VP8, AVS, WMV
Audio coding formats
- WAV, MP2, MP3, AAC, OPUS, FLAC, Vorbis, AMR-NB, AMR-WB, WMA v1, WMA v2
Container formats
- WAV, FLAC, OGG, MOV, ASF, FLV, MP3, MP4, MPEG-TS, Matroska (MKV), AVI, ASS, CONCAT, DTS, AVS
Supported protocols
- HTTP, HTTPS, RTMP, HLS, RTP, RTSP
Sample project
Agora provides an open source sample project MediaPlayer on GitHub. Download it or view the source code for a more detailed example.