#include "mainwindow.h"
#include "ui_mainwindow.h"
#define PLAY 0
#define PAUSE 1
#define KILL 2
#define MP3PATH "/mp3/song"
int songIndex=0; //正在播放的音乐序号
QStringList allFiles ; //所有音乐的数组
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
process=new QProcess();
state=KILL;
add_song();
playStyle=SHUNXU;
connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this, SLOT(finishedSlot(int,QProcess::ExitStatus)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnPre_clicked()
{
songIndex--;
if(songIndex <0)
songIndex = 0; //如果已到最前一首,还点上一首则停在最前一首
QString mp3file=allFiles[songIndex];
myPlay(mp3file);
ui->listWidget->setCurrentRow(songIndex); //设置当前播放的音乐被选中
}
void MainWindow::on_btnPlay_clicked()
{
if(state!=PLAY)//如果没播放则开启播放进程
myPlay("123.mp3");
}
void MainWindow::myPlay(QString mp3file)
{
//如果正在播放或暂停,则需先kill此播放进程,再播放下一首
if(state==PLAY || state == PAUSE)
system("killall -9 madplay");
QString tempstr;
tempstr.push_back("madplay ");
tempstr.push_back("/mp3/song/"+mp3file);
tempstr.push_back(" ");
process->waitForFinished(2000);
process->start(tempstr);
state=PLAY;
ui->lblMsg->setText(mp3file); //界面上显示正在播放的音乐文件名
}
void MainWindow::on_btnPause_clicked()
{
if(state==PLAY){ //如果在播放则暂停
qDebug()<<"PAUSE"<<endl;
system("killall -STOP madplay");
state=PAUSE;
}
else if(state==PAUSE){ //如果在暂停则继续播放
qDebug()<<"CONT"<<endl;
system("killall -CONT madplay");
state=PLAY;
}
}
void MainWindow::on_btnKill_clicked()
{
if(state==PLAY || state== PAUSE){ //如果处于播放状态或暂停状态则杀死进程
qDebug()<<"KILL"<<endl;
system("killall -9 madplay");
state=KILL;
}
}
void MainWindow::on_btnNext_clicked()
{
songIndex++;
if(songIndex > (allFiles.size()-1))
songIndex = (allFiles.size()-1); //如果已到最后一首,还点下一首则停在最后一首
QString mp3file=allFiles[songIndex];
myPlay(mp3file);
ui->listWidget->setCurrentRow(songIndex); //设置当前播放的音乐被选中
}
void MainWindow::add_song(){
QDir directory;
directory.setPath(MP3PATH); //设置mp3文件的路径
QStringList filter;
filter<<"*.mp3"; //设置文件夹过滤,只取.mp3文件
allFiles= directory.entryList(filter); //列出所过满足过滤规则的文件名
ui->listWidget->clear(); //清空播放列表
for(int i=0; i<allFiles.size();i++) //从files中添加播放列表
ui->listWidget->addItem(allFiles[i]);
}
void MainWindow::on_cmbAdd_activated(QString str )
{
if( ui->cmbAdd->currentIndex()==1) //添加单个
{
QStringList list= QFileDialog::getOpenFileNames(this,tr("添加单个"),"/","mp3 files (*.mp3)");
for(int i=0;i<list.size();i++)
{
QString shell= "cp "+ list[i] + " "+ MP3PATH;
system(shell.toLatin1().data()); //调用shell命令复制文件
}
add_song(); //更新界面
}else if( ui->cmbAdd->currentIndex()==2) //添加目录
{
QString dir= QFileDialog::getExistingDirectory(this,tr("添加目录","/"));
QString shell= "cp "+ dir + "/* "+ MP3PATH;
system(shell.toLatin1().data()); //调用shell命令复制文件
add_song(); //更新界面
}
}
void MainWindow::on_cmbDelete_activated(QString str )
{
if(ui->cmbDelete->currentIndex()==1) //删除单个
{
if(ui->listWidget->currentRow()!=-1) //如果有选中
{
if( QMessageBox::question(this,tr("删除提示"),tr("是否真的删除?"),QMessageBox::Yes | QMessageBox::No)==QMessageBox::Yes)
{
QString fileName= ui->listWidget->currentItem()->text();
QString shell="rm -f ";
shell.append(MP3PATH);
shell.append("/");
shell.append(fileName);
system(shell.toLatin1().data());//调用shell命令删除文件
add_song(); //更新界面
}
}
}
else if(ui->cmbDelete->currentIndex()==2)//删除全部
{
if( QMessageBox::question(this,tr("删除提示"),tr("是否真的删除全部?"),QMessageBox::Yes | QMessageBox::No)==QMessageBox::Yes)
{
QString shell="rm -f ";
shell.append(MP3PATH);
shell.append("/*");
system(shell.toLatin1().data());//调用shell命令删除文件
add_song(); //更新界面
}
}
}
int oldValue=0;//用于保存设置静音前的音量
void MainWindow::on_chkMute_clicked()
{
if(ui->chkMute->isChecked()) //如果选中静音,则设置音量为0
{
oldValue=ui->horizontalSlider->value(); //保存设置静音前的音量
ui->horizontalSlider->setValue(0);
}
else //如果未选中静音,则恢复音量
{
ui->horizontalSlider->setValue(oldValue);
}
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
int fd=::open("/dev/mixer",O_RDWR);
::ioctl(fd,MIXER_WRITE(0),&value);
::close(fd);
}
void MainWindow::on_cmbPlayStyle_activated(QString )
{
if(ui->cmbPlayStyle->currentIndex()==0)
this->playStyle=SHUNXU;
else if(ui->cmbPlayStyle->currentIndex()==1)
this->playStyle=SHUIJI;
else
this->playStyle=DANQU;
}
void MainWindow::finishedSlot(int exitCode,QProcess::ExitStatus exitStatus)
{
if(exitStatus==0) //如果一首播放完毕,进程正常结束
{
switch(this->playStyle)
{
case SHUNXU: //顺序播放,如到最后一首,则从新播放
songIndex++;
if(songIndex>allFiles.size()-1)
songIndex=0;
break;
case SHUIJI:
songIndex=::random()%(allFiles.size()); //随机
break;
case DANQU:
//songIndex=songIndex; //单曲不变
break;
}
QString mp3file=allFiles[songIndex];
myPlay(mp3file);
ui->listWidget->setCurrentRow(songIndex);
}
}
pmp.rar_pmp_qt 音乐播放器_willingtx4
版权申诉
56 浏览量
2022-09-23
20:30:17
上传
评论
收藏 45KB RAR 举报
Kinonoyomeo
- 粉丝: 76
- 资源: 1万+
最新资源
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
- 基于物理的非视域成像(NLOS)算法,利用了nerf+python源码+文档说明
- yuluer知更鸟.7z(1).001
- 基于Qt实现医院信息管理系统c++源码+文档说明+数据库(期末大作业)
- 基于python实现的医院信息管理系统完整源码+sql数据库+详细注释(高分课程设计)
- 基于python的眼底图像视杯视盘分割项目源码+文档说明+截图演示+详细注释(高分课程设计)
- ImageBasedModellingEdu-贰壹贰叁零
- DFFmeasurement-数据预处理
- ImageBasedModellingEdu-回文素数c语言
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈