#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
musicPlayer = new QMediaPlayer();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
musicPlayer->stop(); //停止播放上一首
/*1.获取绝对路径absDir*/
QString runPath = QCoreApplication::applicationDirPath();//获取当前exe所在路径
QString absDir = runPath + "/Oh.mp3";
qDebug()<<runPath<<" "<<absDir;
absDir.replace(QString("/"), QString("\\"));/*替换为windown的目录格式*/
ui->textBrowser->setText(absDir);
/*2.创建QMediaPlayer对象指针,通过指针设置文件路径、音量、播放*/
// musicPlayer->setMedia(QUrl::fromLocalFile(absDir));
// musicPlayer->setVolume(ui->lineEdit->text().toInt());
// musicPlayer->play();
boss(ui->textEdit->toPlainText());
}
void MainWindow::on_pushButton_2_clicked()
{
delay(1000);
musicPlayer->stop();
}
void MainWindow::delay(unsigned int msec)
{
QTime _Timer = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < _Timer )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void MainWindow::boss(QString yuepu)
{
QString runPath = QCoreApplication::applicationDirPath();//获取当前exe所在路径
QString absDir = runPath;// + "/Oh.mp3";
int length=yuepu.length();
for(int i=0;i<length;i++)
{
if(yuepu.at(i).toLatin1()>=48 && yuepu.at(i).toLatin1()<=57)
{
absDir=runPath+"/mp3/"+yuepu.at(i)+".mp3";
qDebug()<<absDir;
ui->textBrowser->setText(absDir);
musicPlayer->setMedia(QUrl::fromLocalFile(absDir));
musicPlayer->setVolume(ui->lineEdit->text().toInt());
musicPlayer->play();
delay(500);
}
else if(yuepu.at(i)==' ')
{
delay(700);
}
}
}