1.功能
選擇路徑下的固定視頻格式文件播放,對應(yīng)的音頻和視頻同步播放
2.新建一個不含UI界面的QT工程
特別注意:QT中的路徑名不能含中文
?
設(shè)置一個不含UI界面的工程
取消構(gòu)建目錄
因為要引入視頻播放器的模塊,我們在pro中引入對應(yīng)的模塊
因為要引入布局格式,所以我們引入水平布局和垂直布局,對應(yīng)的.h中引入以下兩個頭文件
同樣的,我們要引入視頻播放的頭文件,需要在pro中引入對應(yīng)的模塊,然后添加對應(yīng)的頭文件
將需要播放聲音的頭文件引入
#include <QMediaPlayer>
一切準備就緒,開始視頻播放器的設(shè)置
--》創(chuàng)建對象。然后在.cpp中的構(gòu)造函數(shù)內(nèi)進行對象的初始化
?
剩下的步驟在代碼中進行中文注釋:
.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QHBoxLayout> //水平布局
#include <QVBoxLayout> //垂直布局
#include <QVideoWidget> //顯示視頻
#include <QMediaPlayer> //播放聲音
#include <QPushButton> //按鈕
#include <QSlider> //滑動條
#include <QStyle> //設(shè)置圖標
#include <QFileDialog> //選擇文件/文件夾
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void chooseVideo();
private:
QMediaPlayer *mediaPlayer;
QVideoWidget *videoWidget;
QVBoxLayout *vbox;
//創(chuàng)建按鈕的兩個對象:選擇視頻按鈕和開始播放按鈕
QPushButton *chooseBtn,*playBtn;
//創(chuàng)建進度條/滑塊的對象
QSlider *slider;
};
#endif // WIDGET_H
?.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//對象實例化
this->mediaPlayer = new QMediaPlayer(this);
this->videoWidget = new QVideoWidget(this);
//設(shè)置播放畫面的最小窗口
this->videoWidget->setMinimumSize(400,400);
//實例化整個窗口的布局---垂直布局
this->vbox = new QVBoxLayout(this);
this->setLayout(this->vbox);
//實例化需要的控件--選擇視頻按鈕
this->chooseBtn = new QPushButton("選擇視頻",this);
//實例化需要的控件--播放按鈕--設(shè)置圖標代替文字
this->playBtn = new QPushButton(this);
this->playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
//實例化需要的控件--滑塊/進度條
this->slider = new QSlider(this);
//默認的進度條布局為垂直的,我們修改為水平方向
this->slider->setOrientation(Qt::Horizontal);
//實例化一個水平布局,將以上空間放在水平布局內(nèi)
QHBoxLayout *hbox = new QHBoxLayout;
//給水平布局添加控件---順序固定,依次放置選擇視頻按鈕、播放按鈕、進度條
hbox->addWidget(this->chooseBtn);
hbox->addWidget(this->playBtn);
hbox->addWidget(this->slider);
//將播放窗口和水平布局都添加到垂直布局中
this->vbox->addWidget(this->videoWidget);
this->vbox->addLayout(hbox);//布局中添加布局,addLayout();
//將選擇視頻對應(yīng)的按鈕和槽函數(shù)進行關(guān)聯(lián)
connect(this->chooseBtn,SIGNAL(clicked()),this,SLOT(chooseVideo()));
}
Widget::~Widget()
{
}
//選擇視頻的槽函數(shù)
void Widget::chooseVideo()
{
//選擇視頻,返回一個播放視頻的名字
QString name = QFileDialog::getSaveFileName(this,"選擇視頻",".","WMV(*.wmv)");
//設(shè)置媒體的聲音
this->mediaPlayer->setMedia(QUrl(name));
//輸出視頻畫面
this->mediaPlayer->setVideoOutput(this->videoWidget);
//播放
this->mediaPlayer->play();
}
?.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-12-11T15:14:38
#
#-------------------------------------------------
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = day4_videoPlayer
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
?最終的視頻播放器(簡單版)樣式如下
------------------------------------------------------------------------------------------------------------------------------------end
?留給讀者的問題:
1.在視頻播放器中添加聲音設(shè)置;
2.添加滑動條拖動改變播放對應(yīng)的進度;
3.將目錄下的視頻放置在界面中,通過雙擊實現(xiàn)視頻的選擇播放;
....
?QT中的類特別多,牢記QT三大護法,對UI界面的繪制和非UI界面的繪制要熟悉,記不住沒關(guān)系,一定要會找,通過庫中的用法去定位自己需要實現(xiàn)的功能,找到對應(yīng)的類和函數(shù)以及其中的枚舉,讀者在學(xué)習時切記要多查閱,多自己實現(xiàn),QT雖然為我們封裝了很多類,如何靈活使用是關(guān)鍵!文章來源:http://www.zghlxwxcb.cn/news/detail-780304.html
加油文章來源地址http://www.zghlxwxcb.cn/news/detail-780304.html
到了這里,關(guān)于QT簡單的視頻播放器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!