libVLC播放失败设置捕获 您所在的位置:网站首页 检测到直播播放异常 libVLC播放失败设置捕获

libVLC播放失败设置捕获

2024-06-29 14:57| 来源: 网络整理| 查看: 265

使用libvlc开发了一个rtmp的播放器,参考网上的文章《使用libVLC实现的播放器:支持双击全屏》很快实现了。

但是使用的时候发现,如果输入一个错误的RTMP地址,或者流中断,播放器端没有任何提示。

翻看代码:

//************************************ // Method: PlayUrl // FullName: CVlcPlayer::PlayUrl // Access: public // Returns: BOOL // Qualifier: // Parameter: CString strUrl // Mark: 播放网络文件/RTSP/RTMP串流等 //************************************ BOOL CVlcPlayer::PlayUrl( CString strUrl ) { if (! m_pVLC_Inst) { Init(); } std::string strPath; strPath = CT2A(strUrl, CP_UTF8); if(strPath.empty() || ! m_pVLC_Inst) { return FALSE; } Stop(); BOOL bRet = FALSE; libvlc_media_t *m; m = libvlc_media_new_location(m_pVLC_Inst, strPath.c_str()); if (m) { if (m_pVLC_Player = libvlc_media_player_new_from_media(m)) { libvlc_media_player_set_hwnd(m_pVLC_Player, m_hWnd); libvlc_media_player_play(m_pVLC_Player); // 事件管理 libvlc_event_manager_t *vlc_evt_man = libvlc_media_player_event_manager(m_pVLC_Player); libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPlaying, ::OnVLC_Event, this); libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnVLC_Event, this); libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnVLC_Event, this); bRet = TRUE; } libvlc_media_release(m); } return bRet; }

在添加事件管理上,只添加了开始播放、进度调整、播放结束这个回调事件,没有增加对错误处理的事件。经过测试,新增了2个事件回调。

libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEncounteredError, ::OnVLC_Event, this); libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerStopped, ::OnVLC_Event, this);

如果输入错误的URL,就会收到上面2个事件,主要处理Error事件就行了。

如果要检测中途有没有中断,一是看有没有收到Stoped回调事件,另外可以注册如下回调,用定时器来判断是不是超过一定时间没更新进度了,来判断是否播放中断。  

//libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerTimeChanged, ::OnVLC_Event, this); libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnVLC_Event, this);

个人谬论,如有错误,欢迎指正。

工程下载 https://download.csdn.net/download/sking007/12718853



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有