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 instance of
IRtcEngine
, create anIMediaPlayer
object. -
To register the playback observer, call
registerPlayerSourceObserverthe
before you join the channel. To unregister the playback observer, callunregisterPlayerSourceObserver
before you leave the channel. -
Implement callbacks for the observer.
-
When joining a channel use
ChannelMediaOptions
to set the media player ID, publish media player's 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 theonPlayerSourceStateChanged
callback reporting the player state asPLAYER_STATE_OPEN_COMPLETED
. -
When a user stops playback or leaves the channel, call
stop
to stop playback, anddestroyMediaPlayer
to release 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 MediaPlayer sample project on GitHub. Download it or view the source code for a more detailed example.