This page tells you how to use the media player APIs to play local or online media locally or to remote users in an Agora channel.
Before proceeding, ensure that you have implemented basic real-time functions in your project. See Start a Call or Start Interactive Live Streaming.
To implement a media player in your project, refer to the following steps.
After the IRtcEngine
initialization, create an IMediaPlayer
object.
void CAgoraMediaPlayer::InitMediaPlayerKit()
{
// Create an Agora media player
m_mediaPlayer = m_rtcEngine->createMediaPlayer().get();
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("createAgoraMediaPlayer"));
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("mediaplayer initialize"));
// Set the view of media player
RECT rc = { 0 };
m_localVideoWnd.GetWindowRect(&rc);
int ret = m_mediaPlayer->setView((agora::media::base::view_t)m_localVideoWnd.GetSafeHwnd());
// Set message receiver of the media player
m_mediaPlayerEvent.SetMsgReceiver(m_hWnd);
}
Call registerPlayerSourceObserver
to register a media observer for the media player.
void CAgoraMediaPlayer::OnBnClickedButtonJoinchannel()
{
...
// Register a media observer
ret = m_mediaPlayer->registerPlayerSourceObserver(&m_mediaPlayerEvent);
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("registerPlayerSourceObserver"));
if (0 == m_rtcEngine->joinChannel(APP_TOKEN, szChannelId.c_str(), 0, options)) {
strInfo.Format(_T("join channel %s, use ChannelMediaOptions"), getCurrentTime());
}
}
else {
// Unregister a media observer
ret = m_mediaPlayer->unregisterPlayerSourceObserver(&m_mediaPlayerEvent);
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("registerPlayerSourceObserver"));
// Leave channel
if (0 == m_rtcEngine->leaveChannel()) {
strInfo.Format(_T("leave channel %s"), getCurrentTime());
}
}
m_lstInfo.InsertString(m_lstInfo.GetCount(), strInfo);
}
Implement callbacks for the media player observer.
// Report media player state changes
LRESULT CAgoraMediaPlayer::OnmediaPlayerStateChanged(WPARAM wParam, LPARAM lParam)
{
CString strState;
CString strError;
switch ((agora::media::base::MEDIA_PLAYER_STATE)wParam)
{
case agora::media::base::PLAYER_STATE_OPEN_COMPLETED:
strState = _T("PLAYER_STATE_OPEN_COMPLETED");
m_mediaPlayerState = mediaPLAYER_OPEN;
int64_t duration;
m_mediaPlayer->getDuration(duration);
m_sldVideo.SetRangeMax((int)duration);
break;
...
}
switch ((agora::media::base::MEDIA_PLAYER_ERROR)lParam)
{
case agora::media::base::PLAYER_ERROR_URL_NOT_FOUND:
strError = _T("PLAYER_ERROR_URL_NOT_FOUND");
break;
...
}
CString strInfo;
strInfo.Format(_T("sta:%s,\nerr:%s"), strState, strError);
m_lstInfo.InsertString(m_lstInfo.GetCount(), strInfo);
return TRUE;
}
// Report the current playback progress
LRESULT CAgoraMediaPlayer::OnmediaPlayerPositionChanged(WPARAM wParam, LPARAM lParam)
{
int64_t * p = (int64_t*)wParam;
m_sldVideo.SetPos((int)*p);
delete p;
return TRUE;
}
Before joining the channel, specify the media player ID and enable publishing media player tracks to share media to remote users in an Agora channel.
void CAgoraMediaPlayer::OnBnClickedButtonPublishVideo()
{
if (m_publishMeidaplayer) {
ChannelMediaOptions op;
op.clientRoleType = CLIENT_ROLE_BROADCASTER;
op.publishMediaPlayerVideoTrack = false;
op.publishMediaPlayerAudioTrack = false;
op.publishMediaPlayerId = m_mediaPlayer->getMediaPlayerId();
int ret = m_rtcEngine->updateChannelMediaOptions(op);
ChannelMediaOptions op2;
op2.clientRoleType = CLIENT_ROLE_BROADCASTER;
op2.publishCameraTrack = true;
ret = m_rtcEngine->updateChannelMediaOptions(op2);
m_publishMeidaplayer = false;
}
else {
ChannelMediaOptions options;
options.clientRoleType = CLIENT_ROLE_BROADCASTER;
options.publishMediaPlayerVideoTrack = true;
options.publishMediaPlayerAudioTrack = true;
options.publishMediaPlayerId = m_mediaPlayer->getMediaPlayerId();
options.publishCameraTrack = false;
options.publishAudioTrack = false;
options.autoSubscribeAudio = false;
options.autoSubscribeVideo = false;
m_rtcEngine->updateChannelMediaOptions(options);
m_publishMeidaplayer = true;
}
}
Open a local or online media file.
void CAgoraMediaPlayer::OnBnClickedButtonOpen()
{
CString strUrl;
CString strInfo;
m_edtVideoSource.GetWindowText(strUrl);
std::string tmp = cs2utf8(strUrl);
switch (m_mediaPlayerState)
{
case mediaPLAYER_READY:
case mediaPLAYER_STOP:
if (tmp.empty())
{
AfxMessageBox(_T("you can fill video source."));
return;
}
// Open media files
m_mediaPlayer->open(tmp.c_str(), 0);
break;
default:
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("can not open player."));
break;
}
}
Play a media file.
void CAgoraMediaPlayer::OnBnClickedButtonPlay()
{
int ret;
switch (m_mediaPlayerState)
{
case mediaPLAYER_PAUSE:
case mediaPLAYER_OPEN:
// Play media files
ret = m_mediaPlayer->play();
if (ret == 0)
{
m_mediaPlayerState = mediaPLAYER_PLAYING;
}
break;
case mediaPLAYER_PLAYING:
// Pause playback
ret = m_mediaPlayer->pause();
if (ret == 0)
{
m_mediaPlayerState = mediaPLAYER_PAUSE;
}
break;
default:
break;
}
}
onPlayerSourceStateChanged
callback reporting the state as PLAYER_STATE_OPEN_COMPLETED
before calling this method to play a media file.Stop the media player and destroy the allocated resources when the user leaves the channel.
void CAgoraMediaPlayer::OnBnClickedButtonStop()
{
if (m_mediaPlayerState == mediaPLAYER_OPEN ||
m_mediaPlayerState == mediaPLAYER_PLAYING ||
m_mediaPlayerState == mediaPLAYER_PAUSE)
{
// Stop play
m_mediaPlayer->stop();
m_mediaPlayerState = mediaPLAYER_STOP;
}
}
void CAgoraMediaPlayer::OnDestroy()
{
// Destroy the media player
CDialogEx::OnDestroy();
UnInitMediaPlayerKit();
}
This section includes in depth information about the methods you used in this page, and links to related pages.
Agora provides an open-source sample project MediaPlayer on GitHub that implements the media player function.