Bootstrap

qt音频实战

一、Qt音频基础知识

1、QT += multimedia

2、QMediaPlayer类:媒体播放器,主要用于播放歌曲、网络收音机等功能。

3、QMediaPlaylist类:专用于播放媒体内容的列表。

二、界面设计

三、代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_player = new QMediaPlayer;
    m_playlist = new QMediaPlaylist;

    //设置播放模式
    m_playlist->setPlaybackMode(QMediaPlaylist::Loop);
    m_player->setPlaylist(m_playlist);

    connect(m_player, SIGNAL(statechanged(QMediaPlayer::State)), this, SLOT(onstatechg(QMediaPlayer::State)));
    connect(m_player, SIGNAL(positionChanged(qint64)), this, SLOT(onpstChg(qint64)));
    connect(m_player, SIGNAL(durationChanged(qint64)), this, SLOT(onDrtchg(qint64)));
    connect(m_playlist, SIGNAL(currentIndexChanged(int)), this, SLOT(onplaylistchg(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onstatechg(QMediaPlayer::State state)    //按钮状态切换
{
    ui->playBtn->setEnabled(!(state == QMediaPlayer::PlayingState));
    ui->pauseBtn->setEnabled(state == QMediaPlayer::PlayingState);
    ui->stopBtn->setEnabled(state == QMediaPlayer::PlayingState);
}
void MainWindow::onplaylistchg(int pos)                   //播放列表更新
{
    ui->listWidget->setCurrentRow(pos);
    QListWidgetItem *item = ui->listWidget->currentItem();
    if(item)
        ui->nameLab->setText(item->text());

}
void MainWindow::onDrtchg(qint64 drt)                     //歌曲时长
{
    ui->progressBar->setMaximum(drt);

    int sec = drt/1000;
    int min = sec/60;
    sec = sec % 60;

    drtTime = QString::asprintf("%d:%d", min, sec);
    ui->progressLab->setText(pstTime+"|"+drtTime);

}
void MainWindow::onpstChg(qint64 pos)                     //歌曲当前播放位置
{
    if(ui->progressBar->isSliderDown())
        return;

    ui->progressBar->setSliderPosition(pos);

    int sec = pos/1000;
    int min = sec/60;
    sec = sec % 60;

    pstTime = QString::asprintf("%d:%d", min, sec);
    ui->progressLab->setText(pstTime+"|"+drtTime);
}


void MainWindow::on_openFileBtn_clicked()
{
    QString currentpath = QDir::currentPath();
    QString dlgtitle = "请选择音频文件:";
    QString strfilter = "所有文件(*.*);;音频文件(*.mp3);;MP3文件(*mp3)";

    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);
        m_playlist->addMedia(QUrl::fromLocalFile(afile)); //添加文件

        QFileInfo fileinfo(afile);
        ui->listWidget->addItem(fileinfo.fileName());
    }

    if(m_player->state()!=QMediaPlayer::PlayingState)
        m_playlist->setCurrentIndex(0);
    m_player->play();
}


void MainWindow::on_playBtn_clicked()
{
    if(m_playlist->currentIndex()<0)
        m_playlist->setCurrentIndex(0);
    m_player->play();
}


void MainWindow::on_pauseBtn_clicked()
{
    m_player->pause();
}


void MainWindow::on_stopBtn_clicked()
{
    m_player->stop();
}


void MainWindow::on_PreviousBtn_clicked()
{
    m_playlist->previous();
}


void MainWindow::on_nextBtn_clicked()
{
    m_playlist->next();
}


void MainWindow::on_volumnBtn_clicked()        //切换音量图标(需要添加图片资源到项目)
{
    bool mute = m_player->isMuted();

    m_player->setMuted(!mute);
    if(mute)
    {
        ui->volumnBtn->setIcon(QIcon(":/icon/volumn.png"));
    }
    else
    {
        ui->volumnBtn->setIcon(QIcon(":/icon/novolume.png"));
    }
}


void MainWindow::on_volumnBar_valueChanged(int value)     //音量调节
{
    m_player->setVolume(value);
}


void MainWindow::on_progressBar_valueChanged(int value)   //进度调节
{
    m_player->setPosition(value);
}

;