一Qt 音频基础知识
- QT += multimedia
- QMediaPlayer 类:媒体播放器,主要用于播放歌曲、网络收音 机等功能。
- QMediaPlaylist 类:专用于播放媒体内容的列表。
二 音频项目实战程序
//版本5.12.8
.proQT += core gui
QT += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetRESOURCES += \images.qrc
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QFileDialog>
#include <QStringList>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;//定义播放对象QMediaPlayer * player;//播放对象QMediaPlaylist *playlist;//播放列表QString drtTime;//歌曲时长QString psTime;//播放位置,进度private slots:void onstatechg(QMediaPlayer::State state);//播放按钮的切换状态void onplaylisting(int pos);//播放列表void onDrtchg(qint64 drt);//歌曲总时间长度void onpstchg(qint64 pos);//播放歌曲当前位置void on_pushButton_open_clicked();void on_pushButton_Player_clicked();void on_pushButton_Pause_clicked();void on_pushButton_Stop_clicked();void on_pushButton_Prev_clicked();void on_pushButton_Next_clicked();void on_pushButton_Volumn_clicked();void on_horizontalSlider_Volumn_valueChanged(int value);void on_horizontalSlider_Speed_valueChanged(int value);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);player = new QMediaPlayer(this);playlist = new QMediaPlaylist(this);playlist->setPlaybackMode(QMediaPlaylist::Loop);//默认循环模式player->setPlaylist(playlist);connect(player,SIGNAL(statechanged(QMediaPlayer::State)),this,SLOT(onstatechg(QMediaPlayer::State)));//信号与槽的链接connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onpstchg(qint64)));connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDrtchg(qint64)));connect(playlist,SIGNAL(currentIndexChanged(int)),this,SLOT(onplaylisting(int)));}MainWindow::~MainWindow()
{delete ui;
}
//播放按钮的切换状态
void MainWindow::onstatechg(QMediaPlayer::State state)
{ui->pushButton_Player->setEnabled(!(state==QMediaPlayer::PlayingState));ui->pushButton_Pause->setEnabled(state==QMediaPlayer::PlayingState);ui->pushButton_Stop->setEnabled(state==QMediaPlayer::PlayingState);
}
//播放列表
void MainWindow::onplaylisting(int pos)
{ui->listWidget->setCurrentRow(pos);//选中当前行QListWidgetItem *item = ui->listWidget->currentItem();if(item)ui->label_Name->setText(item->text());}
//歌曲总时间长度,更新变化进度
void MainWindow::onDrtchg(qint64 drt)
{ui->horizontalSlider_Speed->setMaximum(drt);int sec = drt/1000;//秒int min = sec/60;//分sec = sec%60;//余数为妙//drtTime =QString::asprintf("%d:%d",min,sec);ui->label_Time->setText(psTime+"|"+drtTime);}
//播放歌曲当前位置,更新变化情况
void MainWindow::onpstchg(qint64 pos)
{if(ui->horizontalSlider_Speed->isSliderDown())return;//ui->horizontalSlider_Speed->setSliderPosition(pos);int sec = pos/1000;//秒int min = sec/60;//分sec = sec%60;//余数为妙//psTime =QString::asprintf("%d:%d",min,sec);ui->label_Time->setText(psTime+"|"+drtTime);}
//44:20void MainWindow::on_pushButton_open_clicked()
{//添加歌曲文件QString currentpath = QDir::homePath();//获取当前目录QString dlgtitle = "请选择音频文件";//文件的对话框的标题QString strfilter = "所有文件(*.*);;音频文件(*.mp3 *.wav);;MP3文件(*.mp3);;WAV文件(*.wav)";QStringList filelist = QFileDialog::getOpenFileNames(this,dlgtitle,currentpath,strfilter);if(filelist.count()<1){return;}for(int i = 0;i<filelist.count();i++){//添加文件实现播放QString afile =filelist.at(i);playlist->addMedia(QUrl::fromLocalFile(afile));//添加文件,QFileInfo fileinfo(afile);//将文件添加到 listwidget控件来ui->listWidget->addItem(fileinfo.fileName());}if(player->state()!=QMediaPlayer::PlayingState){playlist->setCurrentIndex(0);}//播放player->play();}void MainWindow::on_pushButton_Player_clicked()
{//判断当前播放哪一个if(playlist->currentIndex()<0)playlist->setCurrentIndex(0);player->play();}void MainWindow::on_pushButton_Pause_clicked()
{//暂停player->pause();
}void MainWindow::on_pushButton_Stop_clicked()
{//停止player->stop();
}void MainWindow::on_pushButton_Prev_clicked()
{//上一曲playlist->previous();
}void MainWindow::on_pushButton_Next_clicked()
{//下一曲playlist->next();
}void MainWindow::on_pushButton_Volumn_clicked()
{//控制静音状态bool mute = player->isMuted();player->setMuted(!mute);if(mute){ui->pushButton_Volumn->setIcon(QIcon(":/new/prefix1/images/volumn.bmp"));}else{ui->pushButton_Volumn->setIcon(QIcon(":/new/prefix1/images/mute.bmp"));}}void MainWindow::on_horizontalSlider_Volumn_valueChanged(int value)
{//调整音量player->setVolume(value);ui->label_VolumeSize->setNum(value);
}void MainWindow::on_horizontalSlider_Speed_valueChanged(int value)
{//调整播放进度player->setPosition(value);}