135 lines
3.6 KiB
C++
135 lines
3.6 KiB
C++
//
|
|
// Created by Administrator on 2023-09-17.
|
|
//
|
|
|
|
#include "CVlcKit.h"
|
|
|
|
CVlcKit::CVlcKit() = default;
|
|
|
|
CVlcKit::~CVlcKit()
|
|
{
|
|
libvlc_media_player_release(m_pMediaPlayer);
|
|
libvlc_release(m_pInstance);
|
|
}
|
|
|
|
libvlc_media_player_t *CVlcKit::getPlayerPoint() const {
|
|
return m_pMediaPlayer;
|
|
}
|
|
|
|
bool CVlcKit::InitVLC() {
|
|
|
|
m_pInstance = libvlc_new(0, nullptr);
|
|
if (!m_pInstance) {
|
|
m_szError = u8"libvlc new failed!";
|
|
return false;
|
|
}
|
|
|
|
m_pMediaPlayer = libvlc_media_player_new(m_pInstance);
|
|
if (!m_pMediaPlayer) {
|
|
libvlc_release(m_pInstance);
|
|
m_szError = u8"libvlc_media_player_new failed!";
|
|
return false;
|
|
}
|
|
m_event = libvlc_media_player_event_manager(m_pMediaPlayer);
|
|
libvlc_event_attach(m_event, libvlc_MediaPlayerPositionChanged, vlc_call, this);
|
|
libvlc_event_attach(m_event, libvlc_MediaPlayerAudioVolume, vlc_call, this);
|
|
|
|
return true;
|
|
}
|
|
|
|
void CVlcKit::vlc_call(const struct libvlc_event_t *p_event, void *p_data) {
|
|
auto* pThis = static_cast<CVlcKit*>(p_data);
|
|
if (!pThis) {
|
|
return;
|
|
}
|
|
switch (p_event->type) {
|
|
case libvlc_MediaPlayerPositionChanged:
|
|
{
|
|
float pos = libvlc_media_player_get_position(pThis->m_pMediaPlayer);
|
|
emit pThis->sigSetTimeSlider(static_cast<int>(pos * 100));
|
|
long long current = libvlc_media_player_get_time(pThis->m_pMediaPlayer) / 1000;
|
|
emit pThis->sigSetStringTime(current);
|
|
break;
|
|
}
|
|
case libvlc_MediaPlayerAudioVolume:
|
|
{
|
|
int vol = libvlc_audio_get_volume(pThis->m_pMediaPlayer);
|
|
emit pThis->sigSetVolumeSlider(vol);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void CVlcKit::stopVideo() {
|
|
if (!m_pMediaPlayer) {
|
|
return;
|
|
}
|
|
if (libvlc_media_player_get_state(m_pMediaPlayer) == libvlc_state_t::libvlc_Playing) {
|
|
libvlc_media_player_stop(m_pMediaPlayer);
|
|
}
|
|
}
|
|
|
|
void CVlcKit::pauseVideo() {
|
|
if (!m_pMediaPlayer) {
|
|
return;
|
|
}
|
|
if (libvlc_media_player_get_state(m_pMediaPlayer) == libvlc_state_t::libvlc_Playing) {
|
|
libvlc_media_player_pause(m_pMediaPlayer);
|
|
}
|
|
}
|
|
|
|
void CVlcKit::playVideo() {
|
|
if (!m_pMediaPlayer) {
|
|
return;
|
|
}
|
|
if (libvlc_media_player_get_state(m_pMediaPlayer) == libvlc_state_t::libvlc_Paused
|
|
|| libvlc_media_player_get_state(m_pMediaPlayer) == libvlc_state_t::libvlc_Stopped) {
|
|
libvlc_media_player_play(m_pMediaPlayer);
|
|
}
|
|
}
|
|
|
|
bool CVlcKit::openVideo(const char* path, void* pWindow) {
|
|
// 设置路径
|
|
m_pMedia = libvlc_media_new_path(m_pInstance, path);
|
|
if (!m_pMedia) {
|
|
m_szError = u8"libvlc_media_new_path failed!";
|
|
return false;
|
|
}
|
|
// 解析media
|
|
libvlc_media_parse(m_pMedia);
|
|
// 获取总时长 (ms)
|
|
m_totalSec = libvlc_media_get_duration(m_pMedia);
|
|
emit sigSetTotalSecond(m_totalSec / 1000);
|
|
// 设置media
|
|
libvlc_media_player_set_media(m_pMediaPlayer, m_pMedia);
|
|
// 设置播放窗口句柄
|
|
libvlc_media_player_set_hwnd(m_pMediaPlayer, pWindow);
|
|
// 释放media
|
|
libvlc_media_release(m_pMedia);
|
|
m_pMedia = nullptr;
|
|
// 播放视频
|
|
libvlc_media_player_play(m_pMediaPlayer);
|
|
return true;
|
|
}
|
|
|
|
std::string CVlcKit::getLastError() const {
|
|
return m_szError;
|
|
}
|
|
|
|
void CVlcKit::setPlayPostion(float pos) {
|
|
if (!m_pMediaPlayer) {
|
|
return ;
|
|
}
|
|
libvlc_media_player_set_position(m_pMediaPlayer, static_cast<float >(pos / 100.0));
|
|
}
|
|
|
|
void CVlcKit::setVolume(int vol) {
|
|
if (!m_pMediaPlayer) {
|
|
return ;
|
|
}
|
|
libvlc_audio_set_volume(m_pMediaPlayer, vol);
|
|
}
|
|
|