QMediaPlayer 类的应用--音频播放器

QMediaPlayer 类:媒体播放器,主要用于播放歌曲、网络收音机等功能。
在这里插入图片描述

在pro文件中添加QT += multimedia

  • QAudioFrePro.pro
#-------------------------------------------------
#
# Project created by QtCreator 2024-01-17T22:18:26
#
#-------------------------------------------------

QT       += core gui

QT +=multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QAudioFrePro
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 \
        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

  • mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
//添加头文件
#include <QMediaPlayer>//媒体播放器
#include <QMediaPlaylist>//播放列表
#include <QFileDialog>//选择播放的歌曲

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QMediaPlayer *player;//播放器的对象
    QMediaPlaylist *playlist;//播放列表

    QString drtTime;//歌曲时长
    QString pstTime;//播放位置
//自定义槽函数,描述歌曲切换的状态
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_Volumnm_clicked();
    void on_horizontalSlider_Volumn_valueChanged(int value);
    void on_horizontalSlider_valueChanged(int value);
};

#endif // MAINWINDOW_H

  • main.cpp
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

  • mainwindow.cpp
#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->setMaximum(drt);

    int sec = drt/1000;//秒
    int min = sec/60; //分
    sec = sec%60; //余数秒

    drtTime = QString::asprintf("%d:%d",min,sec);
    ui->label_process->setText(pstTime+"|"+drtTime);
}
void MainWindow::onpstchg(qint64 pos)//播放歌曲的当前位置、更新变化情况
{
    if(ui->horizontalSlider->isSliderDown())
        return;

    ui->horizontalSlider->setSliderPosition(pos);

    int sec = pos/1000;//秒
    int min = sec/60; //分
    sec = sec%60; //余数秒

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

void MainWindow::on_pushButton_open_clicked()
{
    //添加歌曲文件
    QString currentpath = QDir::homePath();
    QString dlgtitle = "请选择音频文件:";//文件对话框标题
    QString strfilter = "所有文件(*.*);;音频文件(*.mp3 *.wav);;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);
        playlist->addMedia(QUrl::fromLocalFile(afile));//添加文件

        QFileInfo fileinfo(afile);
        ui->listWidget->addItem(fileinfo.fileName());//将文件添加到界面listwidget控件
    }

    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_Volumnm_clicked()
{
    //控制静音状态
    bool mute = player->isMuted();
    player->setMuted(!mute);
    if(!mute)
            ui->pushButton_Volumnm->setIcon(QIcon(":/new/prefix1/Images/mute.bmp"));
    else
        ui->pushButton_Volumnm->setIcon(QIcon(":/new/prefix1/Images/volumn.bmp"));
}

void MainWindow::on_horizontalSlider_Volumn_valueChanged(int value)
{
    player->setVolume(value);
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    player->setPosition(value);
}

  • mainwindow.ui

在这里插入图片描述

  • 运行结果:

在这里插入图片描述

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃秃秃秃哇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值