Stream media to a channel
Users often need to play video and audio files during online social and business interactions. Video SDK enables you to add media playing functionality to your app. This page shows you how to use the media player classes in Video SDK to enable your users to publish media files to a channel.
Understand the tech
When a user selects a media file to play, you open the media file using a media player instance. When the file is ready to be played, you setup the local video container to display the media player output. You update the channel media options to start publishing the media player stream, then 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.
The following figure shows the workflow you need to integrate media player functionality into your app.
Prerequisites
In order to follow this procedure you must have:
- Implemented the SDK quickstart project for Interactive Live Streaming.
- Physical media input devices, such as a camera and a microphone.
- A JavaScript package manager such as npm.
-
A computer with Internet access.
Ensure that no firewall is blocking your network communication.
Project setup
To create the environment necessary to implement playing media files into your app, open the SDK quickstart Interactive Live Streaming project you created previously.
Add a media player to your app
This section shows how to use the Video SDK to implement a media player into your app, step-by-step.
Implement the user interface
In a real-word application, you provide several buttons to enable a user to open, play, pause and stop playing files in the media player. In this page, you use a single Button
to demonstrate the basic media player functions. You also add two div
to display the play progress to the user. To implement this UI, in index.html
, add the following code after <button type="button" id="leave">Leave</button>
:
Handle the system logic
To setup your project to use the media player APIs, take the following steps:
-
Add the required imports
To import the required Agora libraries, in
preload.js
, add the following beforecreateAgoraRtcEngine,
statement: -
Declare the variables you need
To create and manage an instance of the media player and access the UI elements, in
preload.js
, add the following to the list of declarations:
Implement media player functions
To implement playing and publishing media files in your app, take the following steps:
-
Open, play and pause media files
When a user presses the button for the first time, you create an instance of the media player, set its
mediaPlayerObserver
to receive the callbacks, and open the media file. When the user presses the button again, you play the media file. On subsequent button presses, you pause or resume playing the media file alternately. To do this, add the following method topreload.js
beforedocument.getElementById("leave").onclick = async function ()
: -
Manage media player callbacks
The
IMediaPlayerSourceObserver
implements media player callbacks. You create an instance ofonPlayerSourceStateChanged
and register it with the media player instance. When the player state changes, you take appropriate actions to update the UI inonPlayerStateChanged
. You use theonPositionChanged
callback to update the progress bar. To implement these callbacks, add the following code beforeconst EventHandles =
: -
Configure Agora Engine to publish the media player stream
You use
ChannelMediaOptions
and theupdateChannelMediaOptions
method to specify the type of stream to publish. To switch between publishing media player and camera and microphone streams, inpreload.js
, add the following method beforewindow.onload = () =>
: -
Display media player output locally
Setup a
VideoCanvas
and use it in thesetupLocalVideo
method of the Agora Engine to show the media player output locally. To switch between displaying media player output and the camera stream, inpreload.js
, add the following function beforewindow.onload = () =>
: -
Clean up when you close the app
To free up resources when you exit the app, add the following lines to the
document.getElementById("leave").onclick
method beforewindow.location.reload();
:
Test your implementation
To ensure that you have implemented media player features into your app:
-
Generate a temporary token in Agora Console .
-
In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.
-
In preload.js, update
appID
,channel
andtoken
with your values. -
Run the app
Execute the following command in the terminal:
You see your app opens a window named Get started with Interactive Live Streaming.
- Select Host and press Join.
-
Press Open Media File.
Your app starts loading the media file into media player. When the loading finishes, the button text changes to Play Media File.
-
Press Play Media File
You see the media file played both locally and in the web demo app. The progress bar indicates the play progress.
-
Press Pause Playing Media
Media publishing is paused and the camera and microphone publishing is resumed.
-
Press Resume Playing Media
You see that the media file resumes playing.
-
Wait for the media file to finish playing. When the progress bar reaches the end, the media player publishing is stopped and camera and microphone publishing is resumed.
Reference
This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.