2024/10/17 14:17:33
媒体播放器
本文介绍如何调用媒体播放器相关的 API 播放本地或在线媒体资源,或将媒体资源分享给声网频道内的远端用户。
前提条件
在进行操作之前,请确保你已经在项目中实现了基本的实时音视频功能。有关详细信息,请参考实现音视频互动。
实现方法
参考如下步骤,在你的项目中使用媒体播放器:
-
初始化
IRtcEngine
后,创建一个IMediaPlayer
对象。C++void CAgoraMediaPlayer::InitMediaPlayerKit() {
// 创建声网媒体播放器
m_mediaPlayer = m_rtcEngine->createMediaPlayer().get();
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("createAgoraMediaPlayer"));
m_lstInfo.InsertString(m_lstInfo.GetCount(), _T("mediaplayer initialize"));
// 设置播放器渲染视图
RECT rc = {0};
m_localVideoWnd.GetWindowRect(&rc);
int ret = m_mediaPlayer->setView(
(agora::media::base::view_t)m_localVideoWnd.GetSafeHwnd());
// 设置接收播放器事件的窗口
m_mediaPlayerEvent.SetMsgReceiver(m_hWnd);
} -
调用
registerPlayerSourceObserver
方法注册播放观测器。C++void CAgoraMediaPlayer::OnBnClickedButtonJoinchannel() {
// ...
// 注册播放观测器
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 {
// 取消注册播放观测器
ret = m_mediaPlayer->unregisterPlayerSourceObserver(&m_mediaPlayerEvent);
m_lstInfo.InsertString(m_lstInfo.GetCount(),
_T("registerPlayerSourceObserver"));
// leave channel in the engine.
if (0 == m_rtcEngine->leaveChannel()) {
strInfo.Format(_T("leave channel %s"), getCurrentTime());
}
}
m_lstInfo.InsertString(m_lstInfo.GetCount(), strInfo); -
实现媒体播放器观测器的回调。
C++// 报告播放器状态改变
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;
}
// 报告当前播放进度
LRESULT CAgoraMediaPlayer::OnmediaPlayerPositionChanged(WPARAM wParam,
LPARAM lParam) {
int64_t *p = (int64_t *)wParam;
m_sldVideo.SetPos((int)*p);
delete p;
return TRUE;
} -
调用
joinChannel
加入频道时,通过options
参数设置媒体播放器 ID,发布媒体播放器的音视频,将媒体资源分享给声网频道内的远端用户。C++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
打开本地或在线媒体文件。C++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;
}
// 打开媒体文件
m_mediaPlayer->open(tmp.c_str(), 0);
break;
default:
m_lstInfo.InsertString(m_lstInfo.GetCount(),
_T("can not open player."));
break;
}
} -
调用
play
播放媒体文件。如果你需要暂停播放文件,可调用pause
方法。注意请在收到
onPlayerSourceStateChanged
回调报告播放状态为PLAYER_STATE_OPEN_COMPLETED
后再调用play
方法播放媒体文件。C++void CAgoraMediaPlayer::OnBnClickedButtonPlay() {
int ret;
switch (m_mediaPlayerState) {
case mediaPLAYER_PAUSE:
case mediaPLAYER_OPEN:
// 播放媒体文件
ret = m_mediaPlayer->play();
if (ret == 0) {
m_mediaPlayerState = mediaPLAYER_PLAYING;
}
break;
case mediaPLAYER_PLAYING:
// 暂停播放
ret = m_mediaPlayer->pause();
if (ret == 0) {
m_mediaPlayerState = mediaPLAYER_PAUSE;
}
break;
default:
break;
}
} -
当用户离开频道时,停止播放、销毁媒体播放器、取消注册播放观测器,并销毁分配的资源。
C++void CAgoraMediaPlayer::OnBnClickedButtonStop() {
if (m_mediaPlayerState == mediaPLAYER_OPEN ||
m_mediaPlayerState == mediaPLAYER_PLAYING ||
m_mediaPlayerState == mediaPLAYER_PAUSE) {
// 停止播放
m_mediaPlayer->stop();
m_mediaPlayerState = mediaPLAYER_STOP;
}
}
void CAgoraMediaPlayer::OnDestroy() {
// 销毁媒体播放器
CDialogEx::OnDestroy();
UnInitMediaPlayerKit();
}
参考信息
示例项目
声网提供了开源的示例项目供你参考,你可以前往下载或查看其中的源代码。
支持的格式
声网的媒体播放器支持下列格式及协议:
视频编码格式
- H.263、H.264、H.265、MPEG4、MPEG2、RMVB、Theora、VP3、VP8、AVS、WMV
音频编码格式
- WAV、MP2、MP3、AAC、OPUS、FLAC、Vorbis、AMR-NB、AMR-WB、WMA v1、WMA v2
容器格式
- WAV、FLAC、OGG、MOV、ASF、FLV、PM3、MP4、MPEG-TS、Matroska (MKV)、AVI、ASS、CONCAT、DTS、AVS
支持的协议
- HTTP、HTTPS、RTMP、HLS、RTP、RTSP