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 Broadcast Streaming.
- A device running Windows 7 or higher.
- Microsoft Visual Studio 2017 or higher with Desktop development with C++ support.
-
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 Broadcast 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 simple example, you use a single Button
to demonstrate the basic media player functions. You also add a ProgressBar
to display the play progress to the user.
To add the UI elements, take the following steps:
-
Add a slider control to show media progress
To add a slider control to the UI, take the following steps:
-
Go to menu View > Other Windows > Resource View.
If the Resource View window isn't the top-most window, select the Resource View tab to bring it to the top.
-
To view the dialog resources, in Resource View, go to AgoraImplementation.rc > Dialog and double-click
IDD_AGORAIMPLEMENTATION_DIALOG
.The resource opens inside the Dialog Editor.
-
To open Toolbox, go to menu View > ToolBox.
-
From ToolBox, drag Slider Control and drop it onto the surface of the Dialog Editor.
You see a slider in your Dialog Editor.
-
-
Add a Button to the UI
-
From ToolBox, drag Button and drop it onto the surface of the Dialog Editor
-
To change the button label, in Properties, update the Caption field with
Open Media File
.
-
Handle the system logic
To setup your project to use the media player APIs and access the UI elements, take the following steps:
-
Add the required libraries
To import the required Agora libraries, in
AgoraImplementationDlg.h
, add the following header files at the start: -
Declare the variables you need
To create and manage an instance of the media player and access the UI elements, in
AgoraImplementationDlg.h
, add the following toCAgoraImplementationDlg
: -
Reference the UI elements
In
AgoraImplementationDlg.h
, add the following at the end ofOnInitDialog
:
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 implement this workflow, in Dialog Editor, double-click Open Media File. Dialog Editor automatically creates and opens an event listener for you. Add the following code to the event listener you just created: -
Manage media player callbacks
The
IMediaPlayerObserver
implements media player callbacks. You create an instance ofIMediaPlayerObserver
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 setup an event handler, take the following steps:-
In
AgoraImplementationDlg.h
, add the following class beforeAgoraEventHandler
: -
In
AgoraImplementationDlg.h
, add the following toCAgoraImplementationDlg
: -
In
AgoraImplementationDlg.cpp
, add the following after the list of header files: -
In
AgoraImplementationDlg.cpp
, add the following code: -
In
AgoraImplementationDlg.cpp
, add the following methods beforeOnInitDialog
:
-
-
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, take the following steps:-
In
AgoraImplementationDlg.h
, add the following to methodCAgoraImplementationDlg
beforeafx_msg void OnClose();
-
In
AgoraImplementationDlg.cpp
, add the method afterOnInitDialog
:
-
-
Display media player output locally
To display the media file, you call
stopPreview
to stop the local video preview and callsetView
and pass the local view window handle. To switch between displaying media player output and the camera stream, take the following steps:-
In
AgoraImplementationDlg.h
, add the following to methodCAgoraImplementationDlg
beforeafx_msg void OnClose();
-
In
AgoraImplementationDlg.cpp
, add the method afterOnInitDialog
:
-
-
Clean up when you close the app
To free up resources when you exit the app, add the following lines to the
onDestroy
method aftersuper.onDestroy();
:
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 Visual Studio, in
AgoraImplementation.h
, updateappId
,channelName
andtoken
with the values for your temporary token. -
In Visual Studio, click Local Windows Debugger. A moment later you see the project running on your development device.
If this is the first time you run the project, you need to grant microphone and camera access to your app.
-
Press Open Media File.
After a short while, you see the button text changes confirming that the media file is opened successfully.
-
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
Media publishing is paused and the camera and microphone publishing is resumed.
-
Press Resume
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.