VLC支持情况:
操作系统:Windows、WinCE、Linux、MacOSX、BEOS、BSD、Android
访问形式:文件、DVD/VCD/CD、http、ftp、mms、TCP、UDP、RTP、IP组播、IPv6、rtsp
编码格式:MPEG*、DIVX、WMV、MOV、3GP、FLV、H.263、H.264、FLAC
视频字幕:DVD、DVB、Text、Vobsub
视频输出:DirectX、X11、XVideo、SDL、FrameBuffer、ASCII
控制界面:WxWidgets、QT4/5、Web、Telnet
缺点:对Real Video支持不算好,需要额外的插件;不支持3GP的AMR音频格式
VLC库的基本使用
cpp
关键的库函数:
创建实例
libvlc_new()
加载媒体
libvlc_media_new_path()/libvlc_media_new_location()(file:///、http://、rtsp://、screen://)
创建播放器
libvlc_media_player_new_from_media()
设置播放窗口
libvlc_media_player_set_hwnd()
开始播放
libvlc_media_player_play()
获取播放长度---需要播放之后才能获取
libvlc_media_player_get_length()
获取播放媒体的宽/高
libvlc_video_get_width()/libvlc_video_get_height()
获取播放音量
libvlc_audio_get_volume()
设置播放的音量
libvlc_audio_set_volume()
获取播放位置
libvlc_media_player_get_position()
设置播放位置
libvlc_media_player_set_position()
暂停播放
libvlc_media_player_pause()
停止播放
libvlc_media_player_stop()
释放播放器
libvlc_media_player_release()
释放媒体
libvlc_media_release()
释放实例
libvlc_release()
创建流程
实例-->媒体-->播放器-->播放(播放后各种操作)-->释放播放器-->释放媒体-->释放实例
案例代码:
cpp
#include <iostream>
#include "vlc.h"
#include <conio.h>
//编码转换
#include <Windows.h>
std::string Unicode2Utf8(const std::wstring& strIn)
{
std::string str;
int length = ::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), NULL, 0, NULL, NULL);
str.resize(length + 1);
::WideCharToMultiByte(CP_UTF8, 0, strIn.c_str(), strIn.size(), (LPSTR)str.c_str(), length, NULL, NULL);
return str;
}
int main()
{
int argc = 1;
char* argv[2];
argv[0] = (char*)"--ignore-config"; //忽略配置
libvlc_instance_t* vlc_ins = libvlc_new(argc, argv); //①创建实例
std::string path = Unicode2Utf8(L"file:///D:\\C++Project\\VideoPlay\\VideoPlay\\音乐.mp4");
libvlc_media_t* media = libvlc_media_new_location(vlc_ins, path.c_str());
//libvlc_media_t* media = libvlc_media_new_path(vlc_ins, "音乐.mp4"); //②创建媒体
//media = libvlc_media_new_location(vlc_ins, "file:///D:\\C++Project\\VideoPlay\\VideoPlay\\音乐.mp4");
libvlc_media_player_t* player = libvlc_media_player_new_from_media(media); //③创建播放器
do
{
int ret = libvlc_media_player_play(player); //④播放 返回值0开始 -1错误
if (ret == -1) {
printf("error found!\r\n");
break;
}
int vol = -1;
while (vol == -1)
{
Sleep(10);
vol = libvlc_audio_get_volume(player); //获取音量 默认100
}
//只有media解析加载完成,才会有下面的参数
printf("volume is %d\r\n", vol);
libvlc_audio_set_volume(player, 10); //将音量设置到10
libvlc_time_t tm = libvlc_media_player_get_length(player); //获取长度(毫秒数)
printf("%02d:%02d:%02d\r\n", int(tm / 1000 / 3600), int((tm / 1000 / 60) % 60), int(tm / 1000 % 60));
int width = libvlc_video_get_width(player); //获取宽和高
int height = libvlc_video_get_height(player);
printf("width = %d height = %d\r\n", width, height);
while (!_kbhit()) { //检验键盘是否按下 按下为1
printf("%f%%\r", 100.0 * libvlc_media_player_get_position(player)); //获取播放进度
Sleep(500);
}
getchar();
libvlc_media_player_pause(player); //暂停
getchar();
libvlc_media_player_play(player); //继续
getchar();
libvlc_media_player_stop(player); //停止
} while (0);
libvlc_media_player_release(player); //⑤释放播放器
libvlc_media_release(media); //⑥释放媒体
libvlc_release(vlc_ins); //⑦释放实例
return 0;
}