Vlc播放rtsp视频,网上的例子不少,我看后觉得有点不足的地方,就是他们没有设置播放rtsp视频时的参数,
参数设置对播放网络视频是很重要的,如果设置不当,或不设置,可能你的程序就播放不了rtsp视频了。。。
---------------------------------------------------------------
说下开发步骤吧,挺简单的,我的环境:win7 64 + vs2010
首先当然是导入vlc的开发包了:
// vlc
#include "./vlc-2.2.1/include/vlc.h"
#pragma comment(lib, "./vlc-2.2.1/lib/libvlc.lib")
#pragma comment(lib, "./vlc-2.2.1/lib/libvlccore.lib")
-------------------------------------------------------------------------------------
然后定义3个播放rtsp视频时用到的参数:
libvlc_media_t *m_vlcMedia;
libvlc_media_player_t *m_vlcMplay;
libvlc_instance_t *m_vlcInst;
-------------------------------------------------------------------------------
接下来就是播放rtsp视频的代码了:
const char * const vlc_args[] = {
"--demux=h264",
"--ipv4",
"--no-prefer-system-codecs",
"--rtsp-caching=300",
"--network-caching=500",
"--rtsp-tcp",
//"--verbose=2",
//"--extraintf=logger"
};
if (m_vlcInst == NULL)
{
m_vlcInst = libvlc_new(sizeof(vlc_args)/sizeof(vlc_args[0]), vlc_args);
}
if (m_vlcMedia == NULL)
{
// "rtsp://127.0.0.1:1234/vedio"
m_vlcMedia = libvlc_media_new_location(m_vlcInst, strRtspUrl.GetBuffer());
}
if (m_vlcMplay == NULL)
{
m_vlcMplay = libvlc_media_player_new_from_media(m_vlcMedia);
}
HWND hWndVedio = GetDlgItem(IDC_STATIC_VEDIO)->GetSafeHwnd();
libvlc_media_player_set_hwnd(m_vlcMplay, hWndVedio);
libvlc_media_player_play(m_vlcMplay);
--------------------------------------------------------------------------------------------
停止rtsp播放代码如下:
if (m_vlcMplay != NULL)
{
libvlc_media_player_stop(m_vlcMplay);
libvlc_media_player_release(m_vlcMplay);
m_vlcMplay = NULL;
}
if (m_vlcMedia != NULL)
{
libvlc_media_release(m_vlcMedia);
m_vlcMedia = NULL;
}
---------------------------------------------------------------------
当然,退出程序时,记得释放vlc资源哈:
// 释放VLC资源
if (m_vlcMplay != NULL)
{
libvlc_media_player_release(m_vlcMplay);
m_vlcMplay = NULL;
}
if (m_vlcMedia != NULL)
{
libvlc_media_release(m_vlcMedia);
m_vlcMedia = NULL;
}
if (m_vlcInst != NULL)
{
libvlc_release(m_vlcInst);
m_vlcInst = NULL;
}
-----------------------------------------------------------------------------------
效果截图:
-----------------------------------------------------------------------------------
该demo代码下载地址:
Vlc播放rtsp视频demo-src.rar
---------------------
rtsp视频地址:
// rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp
// rtsp://211.139.194.251:554/live/2/13E6330A31193128/5iLd2iNl5nQ2s8r8.sdp
// rtsp://180.168.116.75:554/user=admin&password=&channel=1&stream=0.sdp?
// rtsp://218.204.223.237:554/mobile/1/691C308E18976C87/8y1vfa435degrqk7.sdp
// rtsp://218.204.223.237:554/live/1/6D1E43167B3A7BDA/oby9efo80duh9bjf.sdp
// rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp
// rtsp://211.139.194.251:554/live/2/13E6330A31193128/5iLd2iNl5nQ2s8r8.sdp