Qt音視頻功能實(shí)現(xiàn)方法詳解
Qt 音視頻
在 Qt 中,音頻主要是通過 QSound 類來實(shí)現(xiàn)。但是需要注意的是 QSound 類只支持播放 wav 格式的音頻文件。也就是說如果想要添加音頻效果,那么首先需要將非 wav 格式的音頻文件轉(zhuǎn)換為 wav 格式。
通過幫助手冊查看 QSound 類如下:

注意:使用 QSound 類時,需要添加模塊:multimedia
1. Qt 音頻
核心 API :
play() 開始或繼續(xù)播放當(dāng)前源
示例代碼:
1、首先在 .pro 文件中加入模塊:

2、界面如下:

3、引入聲音文件:



4、widget.cpp 文件如下:
#include <QSound>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// 實(shí)例化對象
QSound* sound = new QSound(":/sound.wav", this);
connect(ui->pushButton, &QPushButton::clicked, [=](){
sound->play(); // 播放
});
}
2. Qt 視頻
在 Qt 中,視頻播放的功能主要是通過 QMediaPlayer 類和 QVideoWidget 類來實(shí)現(xiàn)。在使用這兩個類時要添加對應(yīng)的模塊 multimedia 和 multimediawidgets.
核心 API:
setMedia() 設(shè)置當(dāng)前媒體源。
setVideoOutput() 將QVideoWidget視頻輸出附加到媒體播放器。如果媒體播放器已經(jīng)附加了視頻輸出,將更換?個新的。
示例代碼:
1、首先在 .pro 文件中添加模塊:

2、widget.h 程序如下:
#include <QWidget>
#include <QHBoxLayout> // 水平布局
#include <QVBoxLayout> // 垂直布局
#include <QVideoWidget> // 顯示視頻
#include <QMediaPlayer> // 播放聲音
#include <QPushButton> // 按鈕
#include <QStyle> // 設(shè)置圖標(biāo)
#include <QFileDialog> // 選擇文件/文件夾
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void closeVideo();
private:
QMediaPlayer* mediaPlayer;
QVideoWidget* videoWidget;
QVBoxLayout* vbox;
// 創(chuàng)建兩個按鈕:選擇視頻按鈕和開播放按鈕
QPushButton* chooseBtn, *playBtn;
private:
Ui::Widget *ui;
};
3、widget.cpp 程序如下:
#include <QMediaPlayer>
#include <QSlider>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
// 對象實(shí)例化
mediaPlayer = new QMediaPlayer(this);
videoWidget = new QVideoWidget(this);
// 設(shè)置播放畫面的窗口
videoWidget->setMinimumSize(600, 600);
// 實(shí)例化窗口布局 --- 垂直布局
this->vbox = new QVBoxLayout(this);
this->setLayout(this->vbox);
// 實(shí)例化選擇視頻按鈕
chooseBtn = new QPushButton("選擇視頻", this);
// 實(shí)例化播放按鈕
playBtn = new QPushButton(this);
// 設(shè)置圖標(biāo)代替文件
playBtn->setIcon(this->style()->standardIcon(QStyle::SP_MediaPlay));
// 實(shí)例化一個水平布局,將以上控件放入水平布局中
QHBoxLayout* hbox = new QHBoxLayout;
// 添加控件
hbox->addWidget(chooseBtn);
hbox->addWidget(playBtn);
// 將播放窗口和水平布局都添加到垂直布局中
vbox->addWidget(videoWidget);
// 布局中添加布局
vbox->addLayout(hbox);
// 將選擇視頻對應(yīng)的按鈕和槽函數(shù)進(jìn)行關(guān)聯(lián)
connect(chooseBtn, &QPushButton::clicked, this, &Widget::chooseVideo);
}
void Widget::chooseVideo()
{
// 選擇視頻,返回一個播放視頻的名字
QString name = QFileDialog::getSaveFileName(this, "選擇視頻", ".", "WMV(*.wmv)");
// 設(shè)置媒體聲音
mediaPlayer->setMedia(QUrl(name));
// 輸出視頻畫面
mediaPlayer->setVideoOutput(videoWidget);
// 播放
mediaPlayer->play();
}總結(jié)
到此這篇關(guān)于Qt音視頻功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Qt音視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言用函數(shù)實(shí)現(xiàn)反彈球消磚塊
這篇文章主要為大家詳細(xì)介紹了C語言用函數(shù)實(shí)現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
C++中關(guān)鍵字const的詳細(xì)說明和使用介紹(最全)
const在C/C++中是十分重要的,如果單純理解為“常量”那么你的格局就小了,今天在這里給大家介紹一下const在C++中具體詳細(xì)的用法,需要的朋友可以參考下2025-03-03
C語言中fgetgrent()函數(shù)和fgetpwent()函數(shù)的用法對比
這篇文章主要介紹了C語言中fgetgrent()函數(shù)和fgetpwent()函數(shù)的用法對比,分別用于讀取組格式函數(shù)和讀取密碼格式,需要的朋友可以參考下2015-08-08

