提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、VLC是什么?
VLC是Video Lan Client的缩写,原先是几个法国的大学生做的项目,后来他们把VLC作为了一个开源的项目,吸引了来自世界各国的很多优秀程序员来共同编写和维护VLC,才逐渐变成了现在这个样子
二、使用步骤
1.引入库
集成VLC库后在项目中引入头文件
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include "vlc.h"
2.使用流程
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";
//1.创建实例
libvlc_instance_t* vlc_ins = libvlc_new(argc, argv);
//2.加载媒体
//std::string path = Unicode2Utf8(L"OD.mp4");
//libvlc_media_t* media = libvlc_media_new_path(vlc_ins, path.c_str());
libvlc_media_t* media = libvlc_media_new_location(vlc_ins, Unicode2Utf8(L"file:///D:\\code\\vs-c++_worokspace\\gitLab\\VedioPlay\\VedioPlay\\OD.mp4").c_str());
//3.创建播放器
libvlc_media_player_t* player = libvlc_media_player_new_from_media(media);
//4.设置播放窗口TODO
do
{
//5.开始播放
int ret = libvlc_media_player_play(player);
if (ret == -1)
{
printf("error found\r\n");
break;
}
Sleep(300);//防止media视频资源还没解析完成
//6.获取播放长度
libvlc_time_t time = libvlc_media_player_get_length(player);
printf("%02d:%02d:%02d.%03d\r\n", int(time / 3600000), int((time / 60000)) % 60, int(time / 1000) % 60, int(time) % 1000);
//7.获取播放媒体的宽高
int width =libvlc_video_get_width(player);
int height = libvlc_video_get_height(player);
printf("width = %d: height = %d\r\n", width, height);
//8.获取播放音量TODO
int vol = libvlc_audio_get_volume(player);
printf("音量:%d\r\n", vol);
//8.5 设置播放音量
libvlc_audio_set_volume(player, 10);
//9.获取播放进度
while (!_kbhit())
{
printf("%f%%\r"/* "\r":每次光标移到本行的行首位置处*/, 100.0 * libvlc_media_player_get_position(player));
Sleep(500);
}
//10.设置播放位置TODO
//libvlc_media_player_set_position()
getchar();
//11.暂停播放
libvlc_media_player_pause(player);
getchar();
//5.开始播放
libvlc_media_player_play(player);
//12.停止播放
getchar();
libvlc_media_player_stop(player);
} while (0);
//13.资源释放
libvlc_media_player_release(player);
libvlc_media_release(media);
libvlc_release(vlc_ins);
return 0;
}
总结
音视频的处理不是那么稳定的,需要有容错处理和多次的测试