Qt简单离线音乐播放器

server/2024/9/25 23:13:13/

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

mainwindow.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) {// 将滑块的值转换为时间格式,并更新 timelabelQTime 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:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QMediaPlayer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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:

QT       += core gui
QT       += multimediagreaterThan(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 += target

在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/server/31124.html

相关文章

python学习笔记----面向对象(十)

一、什么是类 类是一个抽象的模板&#xff0c;用于创建具体的实例。可以将类理解为一个蓝图&#xff0c;它定义了一系列对象共有的属性&#xff08;数据&#xff09;和方法&#xff08;函数&#xff09;。类是对一组具有相同属性和功能的对象的抽象。例如&#xff0c;你可以定…

XBoot:基于Spring Boot 2.x的一站式前后端分离快速开发平台

XBoot&#xff1a;基于Spring Boot 2.x的一站式前后端分离快速开发平台 摘要 随着信息技术的迅速发展&#xff0c;快速构建高质量、高可靠性的企业级应用成为了迫切需求。XBoot&#xff0c;作为一个基于Spring Boot 2.x的一站式前后端分离快速开发平台&#xff0c;通过整合微信…

用双目相机实现坐标标定

一&#xff1a;相机参数设置和计算 镜头参数&#xff1a;MF2808-10MP 靶面尺寸2/3 &#xff0c;视场角&#xff08;对角水平垂直&#xff09; 69.758.545.5 焦距&#xff1a;8mm&#xff0c;分辨率&#xff1a;16241240 1.1视场角的计算 图像分辨率越高&#xff0c;双目匹…

SpringData JPA - ORM 框架下,打造高效数据访问层

目录 一、SpringData JPA 概述 1.1、什么是 JPA 1.2、什么是 ORM 1.3、什么是 Hibernate 1.4、JPA 和 Hibernate 的关系 1.5、JPA 的优势 二、SpringData JPA 实战开发 2.1、依赖 2.2、配置文件 2.3、启动类 2.4、创建实体 2.5、基于 JpaRepository 的 CRUD 三、…

如何Vue.js 结合 Element UI 进行表单验证

Vue.js 结合 Element UI 进行表单验证是一个常见的做法。Element UI 提供了一套丰富的表单组件&#xff0c;并内置了表单验证功能。以下是一个简单的示例&#xff0c;展示如何使用 Vue 和 Element UI 进行表单验证&#xff1a; 首先&#xff0c;确保你已经安装了 Vue 和 Eleme…

AIGC在汽车软件开发的应用举例

AIGC(人工智能生成内容)在汽车软件开发领域的应用主要体现在以下几个方面: 个性化和定制化车辆的创造:通过分析大型数据集并生成新内容,AIGC技术使得创造更加个性化和定制化的车辆成为可能。这不仅提高了车辆的安全性和用户体验,还推动了自动驾驶汽车的发展。 智能座舱…

ffmpeg 转换es流成为ps流

目的是将es流转换成为ps流 写入到文件中 #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavutil/avutil.h> #include <libavutil/timestamp.h>int main(int argc, char** argv) {const char* input_filename &qu…

骑缝电子章怎么盖?

盖骑缝电子章通常涉及几个基本步骤&#xff0c;这里提供一个通用的流程&#xff0c;适用于大多数电子文档处理软件&#xff0c;尤其是那些支持电子签名和印章功能的软件&#xff0c;比如Adobe Acrobat Pro DC、e-章宝(易友EU3000智能盖章软件)等。请注意&#xff0c;具体操作可…