简介
libvlc是一款开源媒体播放器库,由 VideoLAN 团队开发。使用libvlc库可快速实现一款播放器,如果要求项目体积比较小,建议使用FFMPEG实现。
libvlc库下载地址
#下载不同平台SDK库
http://download.videolan.org/pub/videolan/vlc/
#直接下载windowsSDK包
http://download.videolan.org/pub/videolan/vlc/3.0.8/win64/vlc-3.0.8-win64.7z
需要依赖的库文件
CMake引入
cmake_minimum_required(VERSION 3.16)
project(vlc_demo)
set(CMAKE_CXX_STANDARD 14)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
endif()
#定制二进制执行文件的输出路径
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/vlc_sdk)
#指定vlc头文件目录
include_directories(${PROJECT_SOURCE_DIR}/vlc_sdk/include)
#指定lib文件存放目录
link_directories(${PROJECT_SOURCE_DIR}/vlc_sdk/lib)
add_executable(vlc_demo main.cpp)
#引入lib文件
target_link_libraries(vlc_demo libvlc libvlccore)
C++代码实现
#include <iostream>
#include <thread>
//解决vlc库在windows缺少SSIZE_T声明
#ifdef _WIN32
#include <basetsd.h>
typedef SSIZE_T ssize_t;
#endif
#include <vlc/vlc.h>
//heliang
int main() {
//1.创建vlc实例
libvlc_instance_t *vlcNew=libvlc_new(0,NULL);
//2.加载本地视频文件
libvlc_media_t *media=libvlc_media_new_path(vlcNew,"E:\\1.mp4");
//3.创建一个流媒体播放器
libvlc_media_player_t *mediaPlayer=libvlc_media_player_new_from_media(media);
//将流媒体播放器绑定到某个窗口句柄上
// libvlc_media_player_set_hwnd(mediaPlayer, (void*)libvlc_WId);
//释放引用次数
libvlc_media_release(media);
//4.开始播放视频
libvlc_media_player_play(mediaPlayer);
//5.防止播放视频退出
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
//获取视频播放的时长
libvlc_time_t duration=libvlc_media_get_duration(media);
//转换秒
duration=duration/1000;
//时
int hour= duration / 3600;
//分
int minute=(duration % 3600)/60;
//秒
int second= duration % 60;
std::cout << hour << ":" << minute << ":" <<second << std::endl;
libvlc_media_player_get_position(mediaPlayer);
}
return 0;
}
网友评论