一Qt 音频基础知识
- QT += multimedia
- QMediaPlayer 类:媒体播放器,主要用于播放歌曲、网络收音 机等功能。
- QMediaPlaylist 类:专用于播放媒体内容的列表。
二 音频项目实战程序
//版本5.12.8
.pro
QT += core gui
QT += multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += 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.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
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_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
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:20
void 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);
}