1、实现切歌,循环播放
mainwindow.h
private slots:void on_nextBt_clicked();void on_frontBt_clicked();
在mainwindow.cpp中
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow){//切歌存储歌单列表QFileInfoList infolist = dir.entryInfoList();//遍历整个目录,获取所有目录文件foreach(QFileInfo info, infolist){//排除.和..文件if(info.fileName() != "." && info.fileName() != ".."){//把所有歌曲添加进入播放列表playlist.addMedia(QUrl::fromLocalFile(dirpath+info.fileName()));}}
}
//上一首
void MainWindow::on_frontBt_clicked()
{/*------------------------------------------------------------ M_Amount = ui->listWidget->count();//获取歌曲总数量 ---- M_Current = ui->listWidget->currentRow(); //获取当前位置 ----- 在这里每次循环的时候,在行列表会重置到原来的位置,所以会出现一直停留在---- 上一首中 --------------------------------------------------------------*/if(M_Current == 0){M_Current = ui->listWidget->count() - 1;}else{M_Current--;}QListWidgetItem *item = ui->listWidget->item(M_Current);item->setSelected(true);player.stop();player.setMedia(QUrl::fromLocalFile(dirpath+"/"+item->text()));player.play();
}//下一首
void MainWindow::on_nextBt_clicked()
{//先判断当前位置是否是尾端,如果是,直接置0if(M_Current + 1== ui->listWidget->count()){M_Current = 0;}else{M_Current++;}QListWidgetItem *item = ui->listWidget->item(M_Current);item->setSelected(true);player.stop();player.setMedia(QUrl::fromLocalFile(dirpath+"/"+item->text()));player.play();}
2、播放和暂停合二为一
在mainwindow.h
void getState(QMediaPlayer::State state); //播放和暂停传信号
在mainwindow.cpp
//播放停止合二为一
void MainWindow::on_pushButton_3_clicked()
{if(player.state() == QMediaPlayer::PlayingState){player.pause();}else {player.play();}}
void MainWindow::getState( QMediaPlayer::State state )
{if(state == QMediaPlayer::PlayingState){ui->pushButton_3->setText(" | |");}else{ui->pushButton_3->setText(" 》");}
}