Qt简单离线音乐播放器

有上传本地音乐文件,播放,暂停,拖拉进度条等功能的播放器。

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QFileDialog>
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    player = new QMediaPlayer(this);
    // 当播放状态改变时,更新按钮的状态
    connect(player,&QMediaPlayer::stateChanged,this,&MainWindow::onStateChanged);
    // 当音乐文件的总时间改变时,更新滑块的范围
    connect(player, &QMediaPlayer::durationChanged, ui->slider, &QSlider::setMaximum);
    // 当播放位置改变时,更新滑块的值
    connect(player, &QMediaPlayer::positionChanged, ui->slider, &QSlider::setValue);
    // 当滑块的值改变时,设置播放位置
    connect(ui->slider, &QSlider::valueChanged, player, &QMediaPlayer::setPosition);

    connect(ui->slider, &QSlider::valueChanged, [this](int value) {
        // 将滑块的值转换为时间格式,并更新 timelabel
        QTime time(0, value / 60, value % 60);
        ui->timelabel->setText(time.toString("mm:ss"));
    });

}

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


void MainWindow::on_playButton_clicked()
{
    //if(player->state() == QMediaPlayer::PlayingState)
    //{
        //player->pause();
    //}else{
        //player->play();
    //}
    player->play();
}

void MainWindow::onStateChanged(QMediaPlayer::State state)
{
    //if(state == QMediaPlayer::PlayingState)
    //{
        //ui->playButton->setText("Pause");
    //}
    //else
    //{
       //ui->playButton->setText("Play");
    //}
}

void MainWindow::on_openAction_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, "Open a File", "", "Audio File(*.mp3)");
    player->setMedia(QUrl::fromLocalFile(filename));
    player->play();
}

void MainWindow::on_pauseButton_clicked()
{
    player->pause();
}


void MainWindow::on_OpenFileButton_clicked()
{
    on_openAction_triggered();
}


void MainWindow::on_slider_actionTriggered(int action)
{

}

void MainWindow::on_timelabel_linkActivated(const QString &link)
{

}

.h:

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMediaPlayer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_timelabel_linkActivated(const QString &link);

    void on_playButton_clicked();


    void on_slider_actionTriggered(int action);

    void on_pauseButton_clicked();

    void onStateChanged(QMediaPlayer::State state);

    void on_openAction_triggered();

    void on_OpenFileButton_clicked();

private:
    Ui::MainWindow *ui;
    QMediaPlayer *player;
};
#endif // MAINWINDOW_H

.pro:

cpp 复制代码
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


相关推荐
重生之我是数学王子1 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----10 小时前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」16 小时前
【qt版本概述】
开发语言·qt
一路冰雨20 小时前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客20 小时前
QT 自定义界面布局要诀
开发语言·qt
码码哈哈0.021 小时前
VSCode 2022 离线安装插件QT VSTOOl报错此扩展不能安装在任何当前安装的产品上。
ide·vscode·qt
feiyangqingyun1 天前
Qt/C++离线地图的加载和交互/可以离线使用/百度和天地图离线/支持手机上运行
c++·qt·qt天地图·qt离线地图·qt地图导航
gz94562 天前
windows下,用CMake编译qt项目,出现错误By not providing “FindQt5.cmake“...
开发语言·qt
「QT(C++)开发工程师」2 天前
Ubuntu 26.04 LTS 大升级:Qt 6 成为未来新引擎
qt
兆。2 天前
python实战案例----使用 PyQt5 构建简单的 HTTP 接口测试工具
爬虫·python·qt