Qt本地視頻播放器
1、使用vlc官方sdk封裝,並在QLabel上面播放
2、首先到vlc官網下載vlc的sdk環境,下載地址:http://download.videolan.org/pub/videolan/vlc/.
(1)、選擇last最近版本:
(2)、選擇win32版本,根據你自己安裝的qt版本來下載:
(3)、最後選win32.7z下載就好了
3、接下來設置好qt使用第三方庫的配置,並把插件目錄放到項目的debug目錄下。
4、接下來把它封裝爲C++類使用
(1)、player.h
#ifndef PLAYER
#define PLAYER
#if 1
#include <vlc/vlc.h>
#include <vlc/libvlc.h>
#include <map>
extern "C"
{
#include <Windows.h>
#include <WinUser.h>
}
class Player
{
public:
Player();
~Player();
public:
/*!
初始化
@return 成功返回true,否則false
*/
bool InitPlayer();
/*!
開始播放
@param path 本地視頻文件路徑
@param handle 控件的HWND
@return 成功返回0,否則!0
*/
int PlayVideo(const char *path, HWND handle);
/*!
停止播放
@param handle 控件的HWND
*/
void StopVideo(HWND handle);
private:
libvlc_instance_t *m_instace = nullptr;
libvlc_media_player_t *m_pPlayer = nullptr;
/// 這裏是因爲我有多個QLabel所以用了map,一個QLabel可以忽略
std::map<HWND,libvlc_media_player_t *> m_map;
};
#endif
#endif // PLAYER
(2)、player.cpp
#include "player.h"
#include <QDebug>
#if 1
Player::Player()
{
}
Player::~Player()
{
}
/// 初始化vlc
bool Player::InitPlayer()
{
m_instace = libvlc_new(0, NULL);
if(NULL == m_instace)
{
return false;
}
return true;
}
/// 開始播放
int Player::PlayVideo(const char *path, HWND handle)
{
EnableWindow(handle,false);
libvlc_media_t *pMedia = libvlc_media_new_path(m_instace, path);
if(NULL == pMedia)
{
return 2;
}
m_pPlayer = libvlc_media_player_new_from_media(pMedia);
if(NULL == m_pPlayer)
{
return 3;
}
libvlc_media_player_set_hwnd(m_pPlayer, handle);
int ret = libvlc_media_player_play(m_pPlayer);
if(0 != ret)
{
libvlc_media_player_stop(m_pPlayer);
libvlc_media_player_release(m_pPlayer);
return 1;
}
/// 一個QLabel對應一個libvlc_media_player_t,就可選擇關閉哪個
m_map[handle] = m_pPlayer;
return 0;
}
/// 停止播放
void Player::StopVideo(HWND handle)
{
libvlc_media_player_stop(m_map[handle]);
libvlc_media_player_release(m_map[handle]);
}
#endif
(3)、使用方法,先調用初始化,在調用播放函數就行了(qt控件也有接口可以獲得HWND):
PlayVideo("path",(HWND)curLabel->winId());
4、效果圖(MP4文件):
(1)、播放前:
(1)、播放後:
想做其他功能就靠你們自己咯