Embedded QT music player based on mplayer

1. Implement functions

2. Music playback interface

2.1 Interface program 

2.1.1 Interface control initialization

2.1.2 Control style program

3. Song list interface

3.1.1 Define QListWidge object pointer in widget.h

3.1.2 Add QlistWidge initialization in the memberInit function

3.1.3 Set its style in the setMusicPlayStyle function

4. Implementation of music playback function

4.1 Get local songs

4.1.1 Display song list

5. Qprocess implements MPlayer

5.1 QT usage process

5.2 QProcess calls MPlayer method

6. Mplayer command list

7. Implement play and pause functions 

7.1 Player initialization

7.2 Playback function implementation

7.3 Implementation of play button function

8. Get the total time and current time

8.1 Add relevant slot functions to the MaplayerInit function

8.2 At the beginning of the playback slot function, send the command to get the total time

8.3 Get the total time and current time in the data slot function

8.4 Send the command to get the current time in the timer slot function

8.5 Start the timer in the playMusic function

8.6 Add timer switch in updatePlayPauseBtnStateSlot function

9. Double-click playback function

9.1 Associating signals and slots

9.2 Writing slot functions

10. Volume setting

10.1 Associating signals and slots

10.2 Implement slot function

11. Song switching

11.1 Playback mode settings

11.1.1 Use enumeration to define playback mode (enumeration defined within class)

11.1.2 Associating signals and slots

11.1.3 Implement slot function

11.2 Previous song

11.2.1 Associating signals and slots

11.2.2 Implement slot function

11.2.3 Slot function sends customized playback content

11.2.4 Associating custom signals and slots

11.2.5 Implement customized signal and slot functions to update and display playback content

11.3 Automatic switching

11.3.1 Associated slot functions

11.3.2 Implement slot function

12. Fast forward and fast rewind

12.1 Associating dragging slider signals and slots

12.2 Implement dragging slider signals and slots

12.3 Associating the release slide signal with the slot

12.4 Realize the signal and slot of releasing the slider

13. Hide player

13.1 Associated slot functions

13.2 Implement slot function

14. Save songs

14.1 Define Settings object pointer

14.2 Add songs to save in list

14.3 Load and save songs

15. Delete songs


1. Implement functions

The functions implemented by music playback are:
1 ) Play / pause function,
2 ) Previous song / next song,
3 ) Set the volume,
4 ) Set playback mode,
5 ) Double-click the song name in the song list to play the music,
6 ) When playing music, the name of the song to be played can be displayed.
7 ) Display the current playing time and the total time of the current music.
8 ) Slide to change the playback progress of music

2. Music playback interface

   The layout of the interface is laid out using the UI interface, and no program code is used for layout. In the program, of course, the program code interface can also be used for layout. The part here is the ui program code layout, and the playlist uses program layout.

2.1 Interface program 

2.1.1 Interface control initialization

//控件初始化
void Widget::memberInit()
{
 QIcon scanIcon(QPixmap(":/music/resource/player/player_icon/open.png"));
 QIcon palyIcon(QPixmap(":/music/resource/player/player_icon/list.png"));
 ui->scanLocalFileBtn->setIconSize(QSize(40,40));
 ui->scanLocalFileBtn->setIcon(scanIcon);
 ui->showPlayListBtn->setIconSize(QSize(40,40));
 ui->showPlayListBtn->setIcon(palyIcon);
 QIcon sequenceIcon(QPixmap(":/music/resource/player/player_icon/列表循
环.png"));
 QIcon onceIcon(QPixmap(":/music/resource/player/player_icon/单曲循环.png"));
 QIcon rangeIcon(QPixmap(":/music/resource/player/player_icon/随机播放.png"));
 QIcon onceSignle(QPixmap(":/music/resource/player/player_icon/单曲一次.png"));
 QIcon stepPlay(QPixmap(":/music/resource/player/player_icon/顺序播放.png"));
 ui->playModelCbox->addItem(stepPlay,"顺序播放");
 ui->playModelCbox->addItem(onceSignle,"单曲一次");
 ui->playModelCbox->addItem(onceIcon,"单曲循环");
 ui->playModelCbox->addItem(sequenceIcon,"循环播放");
 ui->playModelCbox->addItem(rangeIcon,"随机播放");
 //音量滑条的范围 40~100 声音的范围(测试 40 以上有声音)
 ui->volumeSlider->setRange(0,100);
 //设置音量滑条的初始值为 70
ui->volumeSlider->setValue(50);
}

2.1.2 Control style program

void Widget::setMusicPlayStyle()
{
//设置播放器背景图片 通过调色板实现
 QString musicBack = ":/music/resource/player/播放器背景.png";
 playerPalette.setBrush(QPalette::Background,QBrush(QPixmap(musicBack)));
 this->setPalette(playerPalette);
 this->setAutoFillBackground(true);
 //上一首
 ui->preSongBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/prev.png);"
 "border-radius:10px;"
 );
 //下一首
 ui->nextSongBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/next.png);"
 "border-radius:10px;"
 );
 //播放
 ui->playBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/play.png);"
 "border-radius:10px;"
 );
 //播放模式
 ui->playModelCbox->setStyleSheet("background: url(:/music/resource/player/播放
器背景.png);"
 "border: 1px solid gray;"
 "border-radius:5px;"
 "min-width: 6em;"
 "font: 15px;"
 "color: #FFFFFF;"
 );
 //改变播放滑条和音量滑条的样式表
 
ui->playProgressSlider->setStyleSheet("QSlider::groove:horizontal{border:0px;height
:4px;}"
 "QSlider::subpage:horizontal{background:red;}"
 "QSlider::addpage:horizontal{background:lightgray;}"
 
"QSlider::handle:horizontal{background:white;width:10px;border-radius:5px;margin:-
3px 0px -3px 0px;}");
ui->volumeSlider->setStyleSheet( "QSlider::groove:horizontal{border:0px;height:4px;
}"
 "QSlider::subpage:horizontal{background:red;}"
 "QSlider::addpage:horizontal{background:lightgray;}"
 
"QSlider::handle:horizontal{background:white;width:10px;border-radius:5px;margin:-
3px 0px -3px 0px;}");
 //扫描歌曲 播放列表 返回 关闭 透明
 ui->scanLocalFileBtn->setStyleSheet("background: transparent;"
 "border: 1px solid gray;"
 "border-radius:6px;"
 "color: #FFFFFF;"
 "font:16px;"
 "min-width: 4em;"
 );
 ui->showPlayListBtn->setStyleSheet("background: transparent;"
 "border: 1px solid gray;"
 "border-radius:6px;"
 "color: #FFFFFF;"
 "font:16px;"
 "min-width: 4em;"
 );
 ui->closeBtn->setStyleSheet("background: transparent;"
 "border: 1px solid gray;"
 "border-radius:6px;"
 "color: #FFFFFF;"
 "font:16px;"
 "min-width: 4em;"
 );
 ui->blackBtn->setStyleSheet("background: transparent;"
 "border: 1px solid gray;"
 "border-radius:6px;"
 "color: #FFFFFF;"
 "font:16px;"
 "min-width: 4em;"
 );
 //歌曲名
 ui->songNameLb->setStyleSheet("font:18px;""color:#FFFFFF;");

3. Song list interface

 Use a program to create a QListWidget control to display a song list.

3.1.1  Define the QListWidge object pointer in widget.h

QListWidget *showPlayList;

3.1.2  Add QlistWidge initialization in the memberInit function

 //显示列表初始化
 showPlayList = new QListWidget; //列表显示 --通过程序创建控制
 showPlayList->setMinimumSize(400,500);
 showPlayList->setMaximumSize(400,500);
 showPlayList->setWindowTitle("播放列表");
 showPlayList->setWindowIcon(QIcon(QPixmap(":/music/resource/image/music.png")));
 //设置显示列表显示的位置跟音乐部件的显示位置同一个高度,在音乐部件的后面,x 坐
标为音乐部件的宽度
 this->move(500,300);
 showPlayList->move(this->width()+510,this->y());
playListBtnState = true; //false 隐藏,true 显示
 showPlayList->show();

3.1.3  Set its style in the setMusicPlayStyle function

 //设置显示列表的背景
 showPlayList->setStyleSheet("QListWidget{color:#D4D4D4;"
 "font:17px;"
 "backgroundimage:url(:/music/resource/player/playerlist/播放器列表背景.png);}");

4. Implementation of music playback function

  Because Qt 's music playback is based on the Linux platform , if it is implemented using the existing multimedia player QMediaPlayer provided by Qt , it can only be implemented under the Windows platform and is not supported under the Linux platform because Qt5 multimedia player Use QMediaPlayer, and QMediaPlayer uses the gstreamer backend to play media, and gstreamer is not portable to arm . So I finally decided to use QProcess to directly adjust MPlayer on Qt5 to implement the music playback function. Here are the core functions used in the program.

4.1 Get local songs

/****************************************************
* 函数原型:void getLocalFileSots()
* 函数功能:获取本地音乐文件槽函数
*****************************************************/
void Widget::getLocalFileSots()
{
 //获取选择的文件目录名
 QString fileNameDir = QFileDialog::getExistingDirectory(this,"选择音乐文件
","../music");
 if(fileNameDir.isEmpty())
 {
 return;
 }
 //对目录进行扫描操作,需要目录类,利用目录路径构造目录类对象
 //1.利用 带路径的目录名 构造目录类对象
 QDir dir(fileNameDir);
 //2.编写 过滤的文件格式
 QStringList formatList;
 formatList << "*.mp3" << "*.mp4" << "*.wma";
 //3. 创建文件信息列表
 //QFileInfoList infoList;
 //4. 按照过滤的文件格式扫描文件,选择符合格式的文件
 infoList = dir.entryInfoList(formatList);
 if(infoList.isEmpty())
 {
 return;
 }
 //显示列表显示歌名的时候,前面加显示图标,显示图标的位置
 QIcon musicMap(QPixmap(":/music/resource/player/playerlist/音符 1.png"));
 for(int i = 0; i < infoList.count(); i++)
 {
 //需要检测是否重复
 //媒体播放内容列表需要带路径的文件名
 playListString << infoList[i].filePath();
 //显示给用户的列表只需要显示文件名,并在显示列表前面加图标
 QListWidgetItem *songNameList = new
QListWidgetItem(musicMap,infoList[i].fileName());
songNameList->setSizeHint(QSize(400,40));
 showPlayList->addItem(songNameList);
 //打印文件路径 和文件名
 //qDebug() << infoList[i].fileName(); //不带路径的文件名
 //qDebug() << infoList[i].filePath(); //带路径的文件名
 }
 showPlayList->setCurrentRow(0);
}

4.1.1 Display song list

/****************************************************dddddd
* 函数原型:void updatePlayFileListBtnStateSlots()
* 函数功能:更新显示文件列表的状态
* 该显示文件列表要么显示,要么被隐藏
* 说明:
* playListBtnState == true 显示列表
* playListBtnState == false 隐藏列表
*****************************************************/
void Widget::updatePlayFileListBtnStateSlots()
{
 //跟以前状态相反
 playListBtnState = !playListBtnState;
 QIcon showIcon(QPixmap(":/music/resource/player/player_icon/列表.png"));
 QIcon hideIcon(QPixmap(":/music/resource/player/player_icon/隐藏列表.png"));
 switch (playListBtnState)
 {
 case true:
 showPlayList->show(); //显示
 ui->showPlayListBtn->setIcon(hideIcon);
 break;
 case false:
 showPlayList->hide(); //隐藏
 ui->showPlayListBtn->setIcon(showIcon);
 break;
 }
}

5. Qprocess implements MPlayer

5.1 QT usage process

 program:

Widget::Widget(QWidget *parent) :
 QWidget(parent),
 ui(new Ui::Widget)
{
 ui->setupUi(this);
 myProcess = new QProcess(this);
 connect(myProcess,SIGNAL(readyRead()),this,SLOT(doSlotProcessReadyRead()));
}

void Widget::on_btnExec_clicked()
{
 QString cmd = ui->lEditCmd->text();
 QString arg = ui->lEditArg->text();
 QStringList arrayArg = arg.split(" "); //获取所有的参数
 myProcess->start(cmd,arrayArg);
}

void Widget::doSlotProcessReadyRead(){
 while(!myProcess->atEnd()){
 //readLine 返回的是 QByteArray,转为 QString ,replace("\n","")去掉\n
 QString str = QString(myProcess->readLine()).replace("\n","");
 ui->textEdit->append(str);
 }
}

5.2   QProcess calls MPlayer method

We only need to create a new QProcess, and then start the mplayer thread through the start method of QProcess . As follows:
process = new QProcess();
QStringList args;
args << "-slave"; // Use slave mode
args << "-quiet"; // Do not output redundant information
_process->start("mplayer", args); // Start the process and pass in the parameters args

 Note: The mplayer here needs to add the absolute path, which is the location where your current mplayer is stored.

Then in this way, you can start mplayer . If we want to control mplayer , we can use the write method of QProcess to write commands to mplayer, such as the following:
process->write("pause\n");

 If you want to read the message sent by mplayer , use the readLine method of QProcess as follows:

_process->readLine(data,200);

 In this way, we can read a line of information sent by mplayer , and then we only need to parse this data .

6. Mplayer command list

loadfile name // Load media file name
volume 100 1 // Set the volume, the middle volume is 0-100
seek 50 1 // Set the progress, 50 represents the progress size ( percentage ) , 0-100 //0 is a relative seek of +/- <value> seconds (default).
//1 is a seek to <value> % in the movie.
//2 is a seek to an absolute position of <value> seconds.
mute 1/0 // mute switch
pause // play / pause
get_time_length // Get the total time of playing the file, in seconds
seek value // positioning, valu is the number of seconds
get_percent_pos // Get the percentage of playback
get_time_pos // Get the current playing position in seconds
get_file_name // Get the name of the currently playing media file
get_meta_album // Get the album
get_meta_artist // Get the artist
get_meta_comment // Get comments
get_meta_genre // Get the genre
get_meta_title // Get the title
get_meta_year // Get the year

7. Implement play and pause functions 

7.1 Player initialization

In the header file musicplay , after adding the declaration of the relevant members, and then implementing it in the source file, the first is to initialize the player as follows:
//初始化播放器
void Widget::MaplayerInit()
{
 //创建播放进程
 playProcess = new QProcess(this);
 playProcess->setProcessChannelMode(QProcess::MergedChannels);
 program = "/usr/bin/mplayer"; //X86 架构下
 //program = "/usr/local/bin/mplayer"; //mplayer 命令路径,一定要正确,不然无法
播放 –ARM 架构
 playState = false; //播放状态
 playBtnState = false; //播放按钮状态
playIndex = 0; //播放内容下标,跟显示列表的内容要保持一致
}

7.2 Playback function implementation

void Widget::playMusic()
{
 //如果没有加载播放内容
 playProcess->close();
 args.clear();
 args << "-slave"; //使用 slave 模式
 args << "-quiet"; //不要输出冗余信息
 args << playListString[playIndex];
 qDebug() << " playListString[playIndex]: " << playListString[playIndex];
 QString str = "正在播放: " + showPlayList->currentItem()->text();
 ui->songNameLb->setText(str); //显示正在播放的音乐
playProcess->start(program,args); //开始播放
 playProcess->write("volume 70 1\n");
playState = true;
}

7.3 Implementation of play button function

/****************************************************
* 函数原型:void updatePlayPauseBtnStateSlot()
* 函数功能:更新播放暂停按钮状态
*****************************************************/
void Widget::updatePlayPauseBtnStateSlot()
{
 playBtnState = !playBtnState;
 switch(playBtnState)
 {
 case true:
 if(!playState) {
 playMusic();
 }
 else
 {
 playProcess->write("pause\n");
 }
 ui->playBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/暂停.png);"
 "border-radius:10px;"
 );
 break;
 case false:
 playProcess->write("pause\n");
 ui->playBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/播放.png);"
 "border-radius:10px;"
 );
 break;
}
}

8. Get the total time and current time

8.1  Add related slot functions to the MaplayerInit function

//初始化播放器
void Widget::MaplayerInit()
{
 //关联相关的信号,进程启动成功,出错,有数据可读等
connect(playProcess,SIGNAL(started()),this,SLOT(onStartedSlot()));
connect(playProcess,SIGNAL(error(QProcess::ProcessError)),this,SLOT(onErrorSlot()))
;
connect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(onReadDataSlot()));
connect(playProcess,SIGNAL(finished(int)),this,SLOT(onPlayProcessFinishedSlot()));
//创建定时器--定时器的主要功能, 就是用于定时获取当前音乐的播放时间, 然后进行
转换为当前的播放时间
timer = new QTimer(this);
 connect(timer,SIGNAL(timeout()),this,SLOT(onTimeoutSlot()));
}

8.2  At the beginning of the playback slot function, send the command to get the total time

void Widget::onStartedSlot()
{
 qDebug() << "准备播放";
 //获取播放文件的长度以毫秒为单位
 playProcess->write("get_time_length\n");
}

8.3  Obtain the total time and current time in the data slot function

void Widget::onReadDataSlot()
{
 while (playProcess->bytesAvailable()) {
 QByteArray arr = playProcess->readLine();
 QString str = QString::fromLocal8Bit(arr);
 if(str.contains("ANS_LENGTH"))
 {
 //获取时间总长度
 qDebug() << "::"<<str;
 str.remove("\n");
 str.remove("\r");
 str.remove("ANS_LENGTH=");
 qDebug() << "str = " << str ;
 int t = str.toDouble();
 int m = t / 60;
 int s = t % 60;
 tlength = m * 60 + s;
 qDebug() << "tlength " << tlength;
 //设置播放进度的总数值
 ui->playProgressSlider->setMinimum(0);
 ui->playProgressSlider->setMaximum(tlength);
 ui->playProgressSlider->setSingleStep(1);
 QString timeLength;
 timeLength.sprintf("%02d:%02d",m,s);
 ui->totleTimeLb->setText(timeLength);
 }

if(str.contains("ANS_TIME_POSITION") )
 {
 //获取当前时间
 //qDebug() << "当前时间:: " << str ;
 str.remove("\n");
 str.remove("\r");
 double t = QString(str.split("=").at(1)).toDouble();
 //显示当前时间
 int m = t / 60;
 int s = (int)(t+0.5) % 60; //四舍五入防止少时间
 QString timePos;
 timePos.sprintf("%02d:%02d",m,s);
 ui->currentTimeLb->setText(timePos);
 int now = (m * 60 + s);
 //double slider = now *100 /tlength;
 //设置播放进度当前滑条的值
 ui->playProgressSlider->setValue(now);
 }
 }
}

8.4  Send the command to get the current time in the timer slot function

void Widget::onTimeoutSlot()
{
 //获取当前播放的位置,以秒为单位
 playProcess->write("get_time_pos\n");
}

8.5   Start the timer in the playMusic function

void Widget::playMusic()
{
 …..
 // 定时器开启
timer->start(100);
}

8.6  Add timer switch in updatePlayPauseBtnStateSlot function

void Widget::updatePlayPauseBtnStateSlot()
{
 playBtnState = !playBtnState;
 switch(playBtnState)
 {
 case true:
 if(!playState) {
 playMusic(); //第一次播放
 }
 else
 {
 playProcess->write("pause\n");
timer->start(); →暂停后再次播放时启动定时器
 }
…
 break;
 case false:
 playProcess->write("pause\n");
 timer->stop();→暂停播放时关闭定时器
 ….
 break;
}}

9. Double-click playback function

9.1 Associating signals and slots

//显示列表用户双击信号关联指定音乐文件的槽函数
connect(showPlayList,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(doubleClickedToPl
ayerMusicSlots()));

9.2 Writing slot functions

/****************************************************
* 函数原型:void loadMusicToPlayerSlots()
* 函数功能:加载指定音乐到播放列表槽函数
* 用户通过双击选择需要播放的音乐
*****************************************************/
void Widget::doubleClickedToPlayerMusicSlots()
{
 //获取显示列表的当前行
 int row = showPlayList->currentRow();
 playIndex = row;
 playState = false; //双击之后需要播放音乐
 playBtnState = false; //无论之前是什么状态,双击之后必须播放
 updatePlayPauseBtnStateSlot(); //更新播放暂停按钮—实现播放
}

10. Volume setting

10.1 Associating signals and slots

//音量改变信号
connect(ui->volumeSlider,SIGNAL(sliderMoved(int)),this,SLOT(setVolumeSlot(int)));

10.2 Implement slot function

void Widget::setVolumeSlot(int volume)
{
 QString str = QString("volume ");
 if(playProcess->isOpen())
 {
 QString buf;
 buf.setNum(volume);
str += buf + " 1\n";
 //playProcess->write("volume %d 1\n",volume);
 playProcess->write(str.toLocal8Bit());
 }
}

11. Song switching

11.1 Playback mode settings

11.1.1  Use enumeration to define playback mode (enumeration defined within class)

public:
 typedef enum
 {
 ListPlay = 0, //顺序播放
 SingleSongOnce = 1, //单曲一次
 SingleSongLoop = 2, //单曲循环
 PlayListLoop = 3, //列表循环
 RandomPlay = 4, //随机播放
}PlayModel;

11.1.2 Associating signals and slots

//模式选择关联 更新播放按钮槽函数
connect(ui->playModelCbox,SIGNAL(currentIndexChanged(int)),this,SLOT(updatePlayMode
Slots(int)));

11.1.3 Implement slot function

/****************************************************
* 函数原型:void updatePlayModeSlots(int index)
* 函数功能:更新播放模式,获取用户选择的模式
* 用户选择的模式跟系统的模式顺序关联
*****************************************************/
void Widget::updatePlayModeSlots(int index)
{
 switch (index)
 {
 case 0:
 //顺序播放
 playModel = ListPlay;
 break;
 case 1:
 //单曲一次
 playModel = SingleSongOnce;
 break;
 case 2:
 //单曲循环
 playModel = SingleSongLoop;
 break;
case 3:
 //循环播放
 playModel = PlayListLoop;
 break;
 case 4:
 //随机播放
 playModel = RandomPlay;
 break;
 }
}

11.2 Previous song

11.2.1 Associating signals and slots

//上一首
connect(ui->preSongBtn,SIGNAL(clicked(bool)),this,SLOT(playPrevSongSlot()));

11.2.2 Implement slot function

void Widget::playPrevSongSlot()
{
if(playIndex > 0)
 {
 if(playModel == RandomPlay) //随机播放
 {
 qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));//获取时间因子
 playIndex = qrand() %(playListString.count()-1); //产生
playListString.count()-1 以内的随机数
 }
 else
 {
 playIndex--;
 }
 playState = false; //点击上一首之后需要播放音乐
 playBtnState = false; //无论之前是什么状态,点击上一首之后必须播放
 updatePlayPauseBtnStateSlot();
 //发送播放内容改变信号
 emit playConcentChanged(playIndex);
 }else{//回到最后一首
 if(playModel == RandomPlay) //随机播放
 {
 qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));//获取时间因子
 playIndex = qrand() %(playListString.count()-1); //产生
playListString.count()-1 以内的随机数
 }
 else
 {
 playIndex=playListString.count()-1;
}
 playState = false; //点击上一首之后需要播放音乐
 playBtnState = false; //无论之前是什么状态,点击上一首之后必须播放
 updatePlayPauseBtnStateSlot();
 //发送播放内容改变信号
 emit playConcentChanged(playIndex);
 }

11.2.3 Slot function sends customized playback content

signals:
 void playConcentChanged(int index); //播放内容改变信号,播放内容改变,发送改变的
下标

11.2.4 Associating custom signals and slots

//媒体内容列表行改变(下一首、上一首、一首歌播放结束) 关联 显示列表行改变槽函数
connect(this,SIGNAL(playConcentChanged(int)),this,SLOT(updateShowPlayListRow(int)))
;
//双击当前行 播放内容改变(下一首、上一首、一首歌播放结束) 歌曲名更新
connect(showPlayList,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(updateSongTitleSl
ot()));

11.2.5  Implement customized signal and slot functions to update and display playback content

/****************************************************
* 函数原型:void updateShowPlayListRow(int row)
* 函数功能:更新显示列表行的槽函数
* 根据播放内容列表的行改变更新显示列表的行
* 播放内容列表 0 行对应播放列表第一行
*****************************************************/
void Widget::updateShowPlayListRow(int row)
{
 showPlayList->setCurrentRow(row+1);
 //更新歌曲标题
 //内容列表 改变, 歌曲名更新
 updateSongTitleSlot();
}
//内容列表 改变, 歌曲名更新槽函数
void Widget::updateSongTitleSlot()
{
 //当前项的文本
 QString name = showPlayList->currentItem()->text();
 QString str = "正在播放: " + name;
 ui->songNameLb->setText(str);
}

The same principle applies to the next song.

11.3 Automatic switching

After a song is played, corresponding settings must be made according to the current playback mode.

11.3.1 Associated slot functions

connect(playProcess,SIGNAL(finished(int)),this,SLOT(onPlayProcessFinishedSlot()));

11.3.2 Implement slot function

void Widget::onPlayProcessFinishedSlot()
{
 //定时器关闭
 timer->stop();
 playBtnState = false;
 //更新播放暂停按钮
 switch(playBtnState)
{
 case true:
 ui->playBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/暂停.png);"
 "border-radius:10px;"
 );
 break;
 case false:
 ui->playBtn->setStyleSheet("border-image:
url(:/music/resource/player/player_icon/播放.png);"
 "border-radius:10px;"
 );
 break;
 }
 if(ui->playProgressSlider->value() != ui->playProgressSlider->maximum())
 {
 return;
 }
 //如果 if 语句不成立,则说明,一首歌曲播放完成,而不是播放到一半点击下一首
 //qDebug() << "一首歌曲播放完成.......";
 //一首歌曲播放完成之后,根据播放模式选择要播放的音乐
 switch (playModel)
 {
 case ListPlay: //顺序播
 if(playIndex == playListString.count()-1)
 {
 //播放滑条清空。。。。。。
 ui->playProgressSlider->setValue(0);
 playState = false; //播放当前播放的内容,则停止播放
 ui->songNameLb->setText("无歌曲");//显示无歌曲名
 //当前时间和总共时间清零
 ui->currentTimeLb->setText("00:00");
 ui->totleTimeLb->setText("00:00");
 //如果点击播放,默认播放的是第一首歌曲
 playIndex = 0;
 //设置当前行为第行
 showPlayList->setCurrentRow(playIndex);
 return;
 }
 playIndex++;
 break;
 case SingleSongOnce: //单曲一次
 //播放当前播放的内容,则停止播放
 ui->playProgressSlider->setValue(0);
ui->songNameLb->setText("无歌曲");//显示无歌曲名
 //当前时间和总共时间清零
 ui->currentTimeLb->setText("00:00");
 ui->totleTimeLb->setText("00:00");
 playState = false; //点击播放的时候,可以继续以当前的内容进行播放
 return;
 case SingleSongLoop: //单曲循环
 break;
 case PlayListLoop: //循环播放
 if(playIndex == playListString.count()-1)
 {
 playIndex = 0;
 }
 playIndex++;
 break;
 case RandomPlay: //随机播放
 qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));//获取时间因子
 playIndex = qrand() %(playListString.count()-1);
 break;
 }
 playState = false; //点击下一首之后需要播放音乐
 playBtnState = false; //无论之前是什么状态,点击下一首之后必须播放
 updatePlayPauseBtnStateSlot();
 //发送播放内容改变信号
emit playConcentChanged(playIndex);
}

12. Fast forward and fast rewind

12.1 Associating dragging slider signals and slots

//快进快退拖动槽函数
connect(ui->playProgressSlider,SIGNAL(sliderMoved(int)),this,SLOT(updateSongSeekSlo
t(int)));

12.2 Implement dragging slider signals and slots

void Widget::updateSongSeekSlot(int seek)
{
disconnect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(onReadDataSl
ot()));//断开读取数据的槽,避免拖动时重新设置了滑块的值
 moveSliderVal = seek; //记录滑块的值
}

12.3 Associating the release slide signal with the slot

//快进快退松开槽函数
connect(ui->playProgressSlider,SIGNAL(sliderReleased()),this,SLOT(updateSongSeekRel
easedSlot()));

12.4 Realize the signal and slot of releasing the slider

void Widget::updateSongSeekReleasedSlot()
{
 qDebug()<< "Released";
 int seek = (moveSliderVal * 100) / tlength;//百分比的进度
 playProcess->write("pause\n");
 QString str = QString("seek ");
 if(playProcess->isOpen())
 {
 QString buf;
 buf.setNum(seek);
 str += buf + " 1\n";
 playProcess->write(str.toLocal8Bit());
} 
connect(playProcess,SIGNAL(readyReadStandardOutput()),this,SLOT(onReadDataSlot()));
重新连接读取数据的槽
}
14.13 关闭音乐 
1. 关闭事件
void Widget::closeEvent(QCloseEvent *event)
{
 //点击部件的叉号的时候,会自动调用关闭事件函数
 //在关闭事件函数里面做一次释放
 //进程关闭
 playProcess->close();
 //清空音乐显示列表
 showPlayList->clear();
 //清空播放内容列表
 playListString.clear();
 //显示列表部件关闭
 showPlayList->close();
 //发送销毁信号
 qDebug() << "关闭程序。。。。。。";
 //emit musicDestroyed(true);
}

13. Hide player

13.1 Associated slot functions

connect(ui->hideBtn,SIGNAL(clicked(bool)),this,SLOT(hideMusicPlayerSlots()));

13.2 Implement slot function

void Widget::hideMusicPlayerSlots()
{
 //设置显示列表状态
 playListBtnState = true;
 //更新显示列表状态
 updatePlayFileListBtnStateSlots();
this->hide();
}

14. Save songs

Users often have such requirements for applications: requiring it to remember its settings , such as window size, location, some other settings, and another frequently used one, which is recent files , etc. These can all be achieved through Qsettings .
When we create a Qsettings object, we need to pass it two parameters. The first is the name of your company or organization, and the second is the name of your application.

14.1 Define Settings object pointer

QSettings *configIniWrite; //配置文件写指针
QSettings *configIniRead; //配置文件读指针
QSettings *configIniDelete; //配置文件删除指针

14.2 Add songs to save in list

void Widget::getLocalFileSots()
{
…
configIniWrite = new QSettings("PlayList.ini", QSettings::IniFormat);
 for(int i = 0; i < infoList.count(); i++)
 {
 …
 //保存当前歌曲
 configIniWrite->setValue("/song/"+ QString::number(i), 
infoList[i].filePath());//保存歌曲列表
… 
 }
 showPlayList->setCurrentRow(0);
 playListBtnState = true;
showPlayList->show();
}

14.3 Load and save songs

Open the songs saved in the list write a function and call it in the constructor
void Widget::loadPlayListSong()
{
 //打开用户保存的歌曲列表
 configIniRead = new QSettings("PlayList.ini", QSettings::IniFormat);
QIcon musicMap(QPixmap(":/music/resource/player/playerlist/音符 1.png"));
 for(int i=0; ; ++i) //将所有的播放列表显示在列表控件中
 {
//100 是写进的值,所以返回的值也是 100
 if(configIniRead->value("/song/"+QString::number(i),100).toInt()!=100)
 {
 QString path =
configIniRead->value("/song/"+QString::number(i)).toString();
 //媒体播放内容列表需要带路径的文件名
 playListString << path;
 QFileInfo fileInfo;
 fileInfo = QFileInfo(path);
 // QString fileName = fileInfo.fileName();//文件名带后缀
 QString fileName = fileInfo.baseName();//文件名不带后缀
 // QString fileSuffix = fileInfo.suffix();//文件后缀
 //显示给用户的列表只需要显示文件名,并在显示列表前面加图标
 QListWidgetItem *songNameList = new
QListWidgetItem(musicMap,fileName);
 songNameList->setSizeHint(QSize(400,40));
 showPlayList->addItem(songNameList);
 }
 else
 {
 break;
 }
 }
 showPlayList->setCurrentRow(0);
}

15. Delete songs

void Widget::deleteSongBtnSlots()
{
 configIniDelete = new QSettings("PlayList.ini",QSettings::IniFormat);//打开播放
列表配置文件
 int currentRow = showPlayList->currentRow();
 if( currentRow >= 0)
 {
 for (int i=currentRow; ; i++) //删除配置文件的内容
 {
 if(configIniDelete->value("/song/"+QString::number(i+1),100).toInt() !=
100) //如果下一行存在播放文件
 {
 
configIniDelete->setValue("/song/"+QString::number(i),configIniDelete->value("/song
/"+QString::number(i+1)).toString());
}
 else
 {
 configIniDelete->remove("/song/"+QString::number(i));
 break;
 }
 }
 QListWidgetItem* item = showPlayList->takeItem(currentRow); //删除显示列表的
内容
 delete item;
 showPlayList->update(); //更新显示
 playListString.removeAt(currentRow); //删除播放列表的内容
}
}

Finish! ! !

Guess you like

Origin blog.csdn.net/Lushengshi/article/details/130859421