Qt 音频编程实战项目

一Qt 音频基础知识

  1. QT += multimedia
  2. QMediaPlayer 类:媒体播放器,主要用于播放歌曲、网络收音 机等功能。
  3. QMediaPlaylist 类:专用于播放媒体内容的列表。

二 音频项目实战程序

//版本5.12.8
.proQT       += core gui
QT +=   multimedia
greaterThan(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 += targetRESOURCES += \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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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:20void 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);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/3223925.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

7.9数据结构

思维导图 作业 doubleloop.h #ifndef __DOUBLELOOP_H__ #define __DOUBLELOOP_H__#include <stdio.h> #include <stdlib.h>typedef int datatype; typedef struct node {union{int len;datatype data;};struct node *pri;//前驱指针struct node *next;//后继指针…

vue3创建项目

1. 安装node.js&#xff0c;添加环境变量&#xff0c;确保cmd里能使用node命令以及npm命令&#xff1a;node --version npm --version 本人安装的版本如下&#xff1a; 2. 安装vue的脚手架 npm install -g vue/cli 3. 创建vue项目&#xff1a;1&#xff09;使用ui&#xff1…

应急响应——勒索病毒

先上搜索引擎上搜 也可以用360来杀 但是都无法解密 可以解密的&#xff1a; linux

昇思25天学习打卡营第10天|ShuffleNet图像分类

ShuffleNet网络结构 ShuffleNet是一种专为移动设备设计的、计算效率极高的卷积神经网络&#xff08;CNN&#xff09;架构。其网络结构的设计主要围绕减少计算复杂度和提高模型效率展开&#xff0c;通过引入逐点分组卷积&#xff08;Pointwise Group Convolution&#xff09;和…

python reload找不到怎么办

Python 3.0 把 reload 内置函数移到了 imp 标准库模块中。它仍然像以前一样重载文件&#xff0c;但是&#xff0c;必须导入它才能使用。 方法一&#xff1a; from imp import reload reload(module) 方法二&#xff1a; import imp imp.reload(module)

4:表单和通用视图

表单和通用视图 1、编写一个简单的表单&#xff08;1&#xff09;更新polls/detail.html文件 使其包含一个html < form > 元素&#xff08;2&#xff09;创建一个Django视图来处理提交的数据&#xff08;3&#xff09;当有人对 Question 进行投票后&#xff0c;vote()视图…

入门PHP就来我这(高级)19 ~ 捕获sql错误

有胆量你就来跟着路老师卷起来&#xff01; -- 纯干货&#xff0c;技术知识分享 路老师给大家分享PHP语言的知识了&#xff0c;旨在想让大家入门PHP&#xff0c;并深入了解PHP语言。 接着上篇我们来看下sql错误的捕获模式。 1 PDO中捕获SQL语句中的错误 在PDO中有3种方法可以捕…

企业化运维(7)_Zabbix企业级监控平台

官网&#xff1a;Zabbix :: The Enterprise-Class Open Source Network Monitoring Solution ###1.Zabbix部署### &#xff08;1&#xff09;zabbix安装 安装源 修改安装路径为清华镜像 [rootserver1 zabbix]# cd /etc/yum.repos.d/ [rootserver1 yum.repos.d]# vim zabbix.r…

嵌入式c语言——指针加修饰符

指针变量可以用修饰符来修饰

软件开发面试题(C#语言,.NET框架)

1. 解释什么是委托&#xff08;Delegate&#xff09;&#xff0c;并举例说明它在C#中的用法。 委托是一种引用类型&#xff0c;它可以用于封装一个或多个方法。委托对象可以像方法一样调用&#xff0c;甚至可以用于创建事件处理程序。委托是C#中实现事件和回调函数的重要机制。…

Proteus + Keil单片机仿真教程(五)多位LED数码管的静态显示

Proteus + Keil单片机仿真教程(五)多位LED数码管 上一章节讲解了单个数码管的静态和动态显示,这一章节将对多个数码管的静态显示进行学习,本章节主要难点: 1.锁存器的理解和使用; 2.多个数码管的接线封装方式; 3.Proteus 快速接头的使用。 第一个多位数码管示例 元件…

windows JDK11 与JDK1.8自动切换,以及切换后失效的问题

1.windows安装不同环境的jdk 2.切换jdk 3.切换失败 原因&#xff1a;这是因为当我们安装并配置好JDK11之后它会自动生成一个环境变量&#xff08;此变量我们看不到&#xff09;&#xff0c;此环境变量优先级较高&#xff0c;导致我们在切换回JDK8后系统会先读取到JDK11生成的…

vue3中使用provide跨层传值(方法)

1.使用provide inject 跨层实现 祖父组件&#xff1a; provide有两个参数&#xff0c;第一个是我们传递的key&#xff0c;第二个就是value了 孙子组件&#xff1a; const dataList inject(getDataList1)//使用inject接收 const dataList1 dataList.getDataList 页面中使…

Nuxt框架中内置组件详解及使用指南(四)

title: Nuxt框架中内置组件详解及使用指南&#xff08;四&#xff09; date: 2024/7/9 updated: 2024/7/9 author: cmdragon excerpt: 摘要&#xff1a;本文详细介绍了Nuxt 3框架中的两个内置组件&#xff1a;和的使用方法与示例。用于捕获并处理客户端错误&#xff0c;提供…

jvm 06 对象内存结构,指针压缩,调优

01 内存布局 mark word 32bit 4B 64bit 8B 类型指针 klass pointer 开启指针压缩 4B 关闭指针压缩 8B 数组长度 4B 没有这个区域 实例数据 bool 1B 1 true&#xff0c;0 false #define TRUE 1 byte 1B char 2B 1B int 4B float 4B long 8B double 8B 引用类型 开启指针压缩 4B …

基于STM32的气压检测报警proteus仿真设计(仿真+程序+设计报告+讲解视频)

基于STM32的气压检测报警proteus仿真设计 1.主要功能2.仿真3. 程序4. 设计报告5. 资料清单&下载链接资料下载链接&#xff1a; 基于STM32的气压检测报警proteus仿真设计(仿真程序设计报告讲解视频&#xff09; 仿真图proteus 8.9 程序编译器&#xff1a;keil 5 编程语言…

Python实现吃豆人游戏详解(内附完整代码)

一、吃豆人游戏背景 吃豆人是一款由Namco公司在1980年推出的经典街机游戏。游戏的主角是一个黄色的小圆点&#xff0c;它必须在迷宫中吃掉所有的点数&#xff0c;同时避免被四处游荡的幽灵捉到。如果玩家能够吃掉所有的点数&#xff0c;并且成功避开幽灵&#xff0c;就可以进入…

【多线程】wait()和notify()

&#x1f970;&#x1f970;&#x1f970;来都来了&#xff0c;不妨点个关注叭&#xff01; &#x1f449;博客主页&#xff1a;欢迎各位大佬!&#x1f448; 文章目录 1. 为什么需要wait()方法和notify()方法&#xff1f;2. wait()方法2.1 wait()方法的作用2.2 wait()做的事情2…

一文带你彻底搞懂什么是责任链模式!!

文章目录 什么是责任链模式&#xff1f;详细示例SpingMVC 中的责任链模式使用总结 什么是责任链模式&#xff1f; 在我们日常生活中&#xff0c;经常会出现一种场景&#xff1a;一个请求需要经过多个对象的处理才能得到最终的结果。比如&#xff0c;一个请假申请&#xff0c;需…

保姆级教程:Linux (Ubuntu) 部署流光卡片开源 API

流光卡片 API 开源地址 Github&#xff1a;https://github.com/ygh3279799773/streamer-card 流光卡片 API 开源地址 Gitee&#xff1a;https://gitee.com/y-gh/streamer-card 流光卡片在线使用地址&#xff1a;https://fireflycard.shushiai.com/ 等等&#xff0c;你说你不…