After installing mplayer in linux environment, open QT in ubuntu. QT installed version 5.12.9. The icons on QT interface are basically pictures collected by punctual atoms. For the implementation method, refer to the teaching in station B
Brief introduction to mplayer command:
Key operation:
Volume reduction: /, 9
Volume increase: *, 0
Up and down keys: Forward and backward for 1 minute,
Left and right buttons: forward and backward for 10 seconds
mplayer Music/*
This is to play all the Music in the Music directory in order
Command:
-shuffle
Random play
-playlist list.txt
List order playback
-slave
Enable background playback from mode
-loop N
Repeat N times, N=0 will repeat forever
-quiet
Specify mplayer not to print garbled information on the screen
-slave
You can enter the specified command and enter the control
-idle
mplayer does not exit after playing the file to the end of the file
-softvol
Mute may not be set without this parameter
mplayer * < /dev/null &
Background playback, you can add parameters
mplayer * < /dev/null > /dev/null 2>1&
When playing in the background, no parameters can be added, and all standard outputs / inputs generated by mplayer * are lost,
Including standard error output (2 > 1 &), so that even if there is an error, there will be no prompt, and the background will run completely quietly
-In the input command line mode: / / note that it is very important that the end of the instruction sent must \ n end, otherwise it will be invalid
loadfile string // Play the song specified by string. String is the song path + file name (for example: "LoadFile ../ easylove.mp3”).
volume x 1 / / set the volume. X is the volume. 1-100
mute 1/0 / / mute switch. 1: Mute; 0: unmute.
Pause / / pause / cancel pause.
seek value // Fast forward or fast to the number of seconds specified by the parameter value
When value is positive, fast forward; When value is negative, rewind.
get_percent_pos / / get the playback progress of the file (percentage: 0 -- 100).
get_time_pos / / obtain the current location of the file, in seconds, accurate to 1 decimal place.
get_file_name / / get the file name of the file.
get_time_length / / gets the length of the file, in seconds.
get_meta_album / / obtain the metadata of the 'album' of the file.
get_meta_artist / / get the metadata of the 'artist' of the file.
get_meta_comment / / obtain the metadata of the 'comment' of the file.
get_meta_genre / / obtain the metadata of the 'genre' of the file.
get_meta_title / / obtain the metadata of the 'title' of the file.
get_meta_year / / obtain the metadata of the 'year' of the file.
get_ percent_ POS ----- > response format: ANS_PERCENT_POSITION=52
get_ time_ Pos - > response format: ANS_TIME_POSITION=124.2
get_ file_ Name -- > response format: ANS_FILENAME='da.mp3'
get_ time_ Length ---- > response format: ANS_LENGTH=221.00
get_ meta_ Album --- > response format: ANS_META_ALBUM=''
get_ meta_ Artist --- > response format: ANS_META_ARTIST=''
get_ meta_ Comment --- > response format: ANS_META_COMMENT=''
get_ meta_ Genre --- > response format: ANS_META_GENRE=''
get_ meta_ Title --- > response format: ANS_META_TITLE=''
get_ meta_ Year --- > response format: ANS_META_YEAR=''
In the previous section, mplayer has been cross compiled and copied to the development board, which is omitted here
QT Code:
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QPalette> #include <QSlider> #include <QTime> #include <QTimer> #include <QListWidget> #include <QProcess> #include <QFileDialog> #include <QDebug> #include <QLabel> #include <QTextCodec> #include <QString> #include <ctime> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void parts_init(); void QSlider_init(); void QListWidget_init(); void scanSong(); private slots: void pause_clicked(); //void exit_clicked(); void prev_clicked(); void next_clicked(); void favorite_clicked(); void listcircle_clicked(); void menu_clicked(); void back_message_slot(); void getPosTime_timeout(); void musicSlide_slot(int value); void volumeSlide_slot(int value); void volume_clicked(); void volume_timeout(); void addfile_clicked(); void cd_timeout(); void songList_itemClicked(); private: Ui::MainWindow *ui; QProcess *process; QPalette Pic; QFileInfoList songFilelist; QTimer *timer_cd; QTimer *timer_getPosTime; QTimer *timer_vol; int cur_time,length_time,value; QSlider *music_Slider,*volume_Slider;//progress bar QPushButton *prev_button, *pause_button, *next_button, *exit_button, *volume_button, *addfile_button, *favorite_button, *listcircle_button, *menu_button; QListWidget *songList;//playlist QLabel *label_cd; QLabel *lable_SongTitle; //Song name QLabel *lable_Songcurrent_time;//Displays the current time QLabel *lable_SongTotal_time;//Display total time }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" int play_flag = false; int favorite_flag = false; int menu_flag = false; int circle_play = true;//false = random play, true = loop list static int i =1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->setWindowTitle("music player "); this->setFixedSize(800,500); //Pic.setBrush(QPalette::Background,QBrush(QPixmap(":/pic/musicback0.jpeg").scaled(this->size()))); //this->setPalette(Pic); process = new QProcess; timer_cd = new QTimer; connect(timer_cd,&QTimer::timeout,this,&MainWindow::cd_timeout); timer_getPosTime = new QTimer; connect(timer_getPosTime,&QTimer::timeout,this,&MainWindow::getPosTime_timeout); timer_getPosTime->start(900); parts_init(); QSlider_init(); QListWidget_init(); connect(pause_button,&QPushButton::clicked,this,&MainWindow::pause_clicked);//I have to use the connect writing method of QT5 high version here connect(prev_button,&QPushButton::clicked,this,&MainWindow::prev_clicked); connect(next_button,&QPushButton::clicked,this,&MainWindow::next_clicked); //connect(exit_button,&QPushButton::clicked,this,&MainWindow::exit_clicked); connect(favorite_button,&QPushButton::clicked,this,&MainWindow::favorite_clicked); connect(listcircle_button,&QPushButton::clicked,this,&MainWindow::listcircle_clicked); connect(menu_button,&QPushButton::clicked,this,&MainWindow::menu_clicked); connect(songList,&QListWidget::doubleClicked,this,&MainWindow::songList_itemClicked); connect(process,&QProcess::readyReadStandardOutput,this,&MainWindow::back_message_slot); //connect(music_Slider,&QSlider::sliderMoved,this,&MainWindow::musicSlide_slot);// The value parameter cannot be passed in. Write it differently first connect(volume_Slider,&QSlider::valueChanged,this,&MainWindow::volumeSlide_slot); connect(volume_button,&QPushButton::clicked,this,&MainWindow::volume_clicked); connect(timer_vol,&QTimer::timeout,this,&MainWindow::volume_timeout); connect(addfile_button,&QPushButton::clicked,this,&MainWindow::addfile_clicked); } MainWindow::~MainWindow() { delete process; delete ui; } void MainWindow::parts_init() { QFont font; label_cd = new QLabel(this); label_cd->setGeometry(390,30,300,300); label_cd->setStyleSheet("border-image: url(:/images/cd.png);"); lable_SongTitle = new QLabel(this); lable_SongTitle->setGeometry(310,360,350,25); font.setPointSize(12); lable_SongTitle->setFont(font); lable_SongTitle->setStyleSheet("QLabel{color:white;}QLabel{background:transparent}"); lable_SongTitle->setText("music PLAY"); lable_SongTitle->setAlignment(Qt::AlignVCenter|Qt::AlignLeft); lable_Songcurrent_time = new QLabel(this);; lable_Songcurrent_time->setGeometry(650,365,60,20); font.setPointSize(10); lable_Songcurrent_time->setFont(font); lable_Songcurrent_time->setStyleSheet("QLabel{color:white;}QLabel{background:transparent}"); lable_Songcurrent_time->setText("00:00 / "); lable_Songcurrent_time->setAlignment(Qt::AlignVCenter|Qt::AlignLeft); lable_SongTotal_time = new QLabel(this); lable_SongTotal_time->setGeometry(700,365,60,20); font.setPointSize(10); lable_SongTotal_time->setFont(font); lable_SongTotal_time->setStyleSheet("QLabel{color:white;}QLabel{background:transparent}"); lable_SongTotal_time->setText("00:00"); lable_SongTotal_time->setAlignment(Qt::AlignVCenter|Qt::AlignLeft); prev_button = new QPushButton; prev_button->setFixedSize(75,75);//Prevent it from getting bigger or smaller prev_button->setStyleSheet("border-image: url(:/images/btn_previous.png);"); prev_button->setIconSize(QSize(75,75));//Set icon size prev_button->setFlat(true); prev_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border prev_button->setParent(this);//The button must be set to the parent window, otherwise it will not be displayed prev_button->move(20,400); pause_button = new QPushButton; pause_button->setFixedSize(75,75);//Prevent it from getting bigger or smaller //pause_ button->setIcon(QIcon(":/images/btn_play.png"));// This click effect is not good-looking pause_button->setStyleSheet("border-image: url(:/images/btn_play.png);"); pause_button->setIconSize(QSize(75,75));//Set icon size pause_button->setFlat(true); pause_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border pause_button->setParent(this); pause_button->move(100,400); next_button = new QPushButton; next_button->setFixedSize(75,75);//Prevent it from getting bigger or smaller next_button->setStyleSheet("border-image: url(:/images/btn_next.png);"); next_button->setIconSize(QSize(75,75));//Set icon size next_button->setFlat(true); next_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border next_button->setParent(this); next_button->move(180,400); /* exit_button = new QPushButton; exit_button->setFixedSize(75,75);//Prevent it from getting bigger or smaller exit_button->setIcon(QIcon(":/images/btn_exit.png")); exit_button->setIconSize(QSize(75,75));//Set icon size exit_button->setFlat(true); exit_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border exit_button->setParent(this); exit_button->move(760,450); */ addfile_button = new QPushButton; addfile_button->setFixedSize(43,43);//Prevent it from getting bigger or smaller addfile_button->setStyleSheet("border-image: url(:/images/openfile.png);"); addfile_button->setIconSize(QSize(43,43));//Set icon size addfile_button->setFlat(true); addfile_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border addfile_button->setParent(this); addfile_button->move(200,0); addfile_button->setVisible(false); favorite_button = new QPushButton; favorite_button->setFixedSize(25,25);//Prevent it from getting bigger or smaller favorite_button->setIcon(QIcon(":/images/btn_favorite_no.png")); favorite_button->setIconSize(QSize(25,25));//Set icon size favorite_button->setFlat(true); favorite_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border favorite_button->setParent(this); favorite_button->move(310,430); listcircle_button = new QPushButton; listcircle_button->setFixedSize(25,25);//Prevent it from getting bigger or smaller listcircle_button->setIcon(QIcon(":/images/btn_listcircle.png")); listcircle_button->setIconSize(QSize(25,25));//Set icon size listcircle_button->setFlat(true); listcircle_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border listcircle_button->setParent(this); listcircle_button->move(388,430); menu_button = new QPushButton; menu_button->setFixedSize(25,25);//Prevent it from getting bigger or smaller menu_button->setIcon(QIcon(":/images/btn_menu1.png")); menu_button->setIconSize(QSize(25,25));//Set icon size menu_button->setFlat(true); menu_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border menu_button->setParent(this); menu_button->move(466,430); timer_vol = new QTimer(this);//Volume display timer volume_button = new QPushButton; volume_button->setFixedSize(25,25);//Prevent it from getting bigger or smaller volume_button->setIcon(QIcon(":/images/btn_volume.png")); volume_button->setIconSize(QSize(25,25));//Set icon size volume_button->setFlat(true); volume_button->setFocusPolicy(Qt::NoFocus);//Remove dashed border volume_button->setParent(this); volume_button->move(544,430); songList = new QListWidget(this); songList->clear(); //clear list songList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //Turn off the horizontal scroll bar songList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //Turn off vertical scroll bar songList->setFrameShape(QFrame::NoFrame); //Remove border songList->setGeometry(0,35,230,370);//Set song list location and size songList->setVisible(false); } void MainWindow::QSlider_init() { //Playback progress slider music_Slider = new QSlider(Qt::Horizontal, this); //Set as horizontal slider music_Slider->setGeometry(310,385,450,40); //Set position and size music_Slider->setRange(0,100); music_Slider->setValue(0); music_Slider->setTracking(false); Disable tracking music_Slider->setStyleSheet( //Set the slider style. If it is written to the qss style file, it cannot be commented //Slide bar "QSlider::groove:horizontal{" " border: 1px solid #999999;" " height: 5px; " "position: absolute;" "border-radius :3px;" " background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);" " margin: 1px 0;" " left: 3px; right: 3px;" "}" //Small slider "QSlider::handle:horizontal {" " background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);" "border: 1px solid #5c5c5c;" "width: 12px;" " height: 12px; " "margin: -2px -1; " " border-radius: 4px;" "}" //Slide the slider over to change the style " QSlider::add-page:horizontal {" "border-radius :2px;" " margin: 1px 1;" "background: white;" "}" "QSlider::sub-page:horizontal {" "border-radius :2px;" " margin: 1px 1;" " background-color:#86dfc6;" "}" ); connect(music_Slider,&QSlider::sliderMoved,[=](int value){ //pause_button->setIcon(QIcon(":/images/btn_pause.png")); QString msg = "seek "+QString::number(value)+" 1\n"; QByteArray ba = msg.toLatin1(); process->write(ba.data()); }); //Volume slider settings volume_Slider = new QSlider(Qt::Horizontal, this); //Set as horizontal slider volume_Slider->setGeometry(575,433,180,20); //Set position and size volume_Slider->setRange(0,100); volume_Slider->setValue(70);//There is no command to return the real-time volume, which is set to 70 by default volume_Slider->setTracking(true); volume_Slider->setVisible(false);//Disable tracking } void MainWindow::back_message_slot() { while (process->canReadLine()) { QString message(process->readLine());//You can also use the readAll() function, which is a little different, but there is no difference here //qDebug()<<message; QStringList message_list = message.split("="); //QString remessage_cmd = message_list[0];// In this way, the following procedure will make mistakes and leave evidence. I didn't know at the beginning //QString remessage_value = message_list[1]; if(message_list[0] == "ANS_TIME_POSITION"){ music_Slider->setValue(100*cur_time/length_time); cur_time = message_list[1].toDouble(); QString textl = QString("%1%2:%3%4 /") .arg(cur_time/60/10) .arg(cur_time/60%10) .arg(cur_time%60/10) .arg(cur_time%60%10); lable_Songcurrent_time->setText(textl); QString textr = QString("%1%2:%3%4") .arg(length_time/60/10) .arg(length_time/60%10) .arg(length_time%60/10) .arg(length_time%60%10); lable_SongTotal_time->setText(textr); } else if (message_list[0] == "ANS_LENGTH") { length_time = message_list[1].toDouble();//Returns seconds } else if (message_list[0] == "ANS_PERCENT_POSITION") { //Returns the playback progress of the current song, in percentage 0-100 value = message_list[1].toInt(); //qDebug()<<value; music_Slider->setValue(value); if(value == 99){ //Debug some songs that will not reach 100, then set it to 99 pause_button->setIcon(QIcon(":/pic/pause.png")); //After playing, set it to pause icon next_clicked(); } } } } void MainWindow::getPosTime_timeout() { if(play_flag == true){ process->write("get_time_length\n"); process->write("get_time_pos\n"); process->write("get_percent_pos\n"); } } void MainWindow::pause_clicked() { process->write("pause\n"); if(play_flag == true){ timer_cd->stop(); pause_button->setIcon(QIcon(":/images/btn_play.png")); play_flag = false; } else if(play_flag == false){ timer_cd->start(); pause_button->setIcon(QIcon(":/images/btn_pause.png")); play_flag = true; } } void MainWindow::prev_clicked() { /*timer_getPosTime->stop(); timer_getPosTime->start(900); music_Slider->setValue(0);*/ int n; play_flag = true; pause_button->setIcon(QIcon(":/images/btn_pause.png")); if(circle_play == false){ qsrand(time(NULL)); n = qrand() % songList->count(); } process->close(); int curRow = 0; curRow = songList->currentRow(); if(curRow-1 >= 0 ){ songList->setCurrentRow(curRow-1); }else { songList->setCurrentRow(songList->count()-1); } curRow = songList->currentRow(); if(circle_play == false){ curRow = n; //qDebug()<<curRow; } //qDebug()<<songFilelist[curRow].filePath(); QString cmd = QString("mplayer -quiet -slave %1").arg(songFilelist[curRow].filePath()); songList->setCurrentRow(curRow); QString tmpname = songFilelist[curRow].fileName(); QStringList list = tmpname.split("."); QString soundname = list[0]; lable_SongTitle->setText(soundname); process->start(cmd); } void MainWindow::next_clicked() { /*timer_getPosTime->stop(); timer_getPosTime->start(900); music_Slider->setValue(0);*/ int n; play_flag = true; pause_button->setIcon(QIcon(":/images/btn_pause.png")); if(circle_play == false){ qsrand(time(NULL)); n = qrand() % songList->count(); } process->close(); int curRow = 0; curRow = songList->currentRow(); if(curRow+1 < songList->count()){ songList->setCurrentRow(curRow+1); }else { songList->setCurrentRow(0); } curRow = songList->currentRow(); if(circle_play == false){ curRow = n; //qDebug()<<curRow; } //qDebug()<<song_info_list[curRow].filePath(); QString cmd = QString("mplayer -quiet -slave %1").arg(songFilelist[curRow].filePath()); songList->setCurrentRow(curRow); QString tmpname = songFilelist[curRow].fileName(); QStringList list = tmpname.split("."); QString soundname = list[0]; lable_SongTitle->setText(soundname); process->start(cmd); } /* void MainWindow::exit_clicked() { process->close(); exit(0); } */ void MainWindow::musicSlide_slot(int value) { QString msg = "seek "+QString::number(value)+" 1\n"; QByteArray ba = msg.toLatin1(); process->write(ba.data()); } void MainWindow::volumeSlide_slot(int value) { timer_vol->stop(); QString msg = QString("volume %1 1\n").arg(value); QByteArray ba = msg.toLatin1(); process->write(ba.data()); timer_vol->start(5000); } void MainWindow::volume_clicked() { volume_Slider->setVisible(true); timer_vol->start(5000);//Stay for 5 seconds } void MainWindow::volume_timeout() { timer_vol->stop(); volume_Slider->setVisible(false); } void MainWindow::QListWidget_init() { scanSong(); QListWidgetItem *item ; for (int i = 0; i < songFilelist.size(); ++i) { //songList->addItem(QFileInfo(List[i]).fileName());// Add (song) list to songlist with addItem function songList->addItem(songFilelist[i].fileName()); item = songList->item(i); item->setSizeHint(QSize(40,40)); item->setForeground(Qt::white); item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); } } void MainWindow::songList_itemClicked() { play_flag = true; if(play_flag == true){ pause_button->setIcon(QIcon(":/images/btn_pause.png")); timer_cd->start(1000); } process->close(); int clickline = 0; clickline = songList->currentRow(); //qDebug() <<clickline; QString tmpname = songFilelist[clickline].fileName(); QStringList list = tmpname.split("."); QString soundname = list[0]; lable_SongTitle->setText(soundname); QString path = songFilelist[clickline].filePath(); //Absolute path //qDebug() <<path; QString cmd = QString("mplayer -quiet -slave %1").arg(path); process->start(cmd); } void MainWindow::scanSong() { QDir dir("/home/jdr/mp3");//Specify the path where the Song Directory needs to be scanned if (dir.exists()) { QStringList filter; filter << "*.mp3" << "*.wav"; // File containing all. MP3. Wav suffixes songFilelist = dir.entryInfoList(filter, QDir::Files); // Obtain all. MP3. Wav suffix files in the directory } } void MainWindow::addfile_clicked() { //The parameters in the parentheses of getOpenFileNames are: specify the parent class, title, directory displayed after opening by default, and file filter QStringList newFileList = QFileDialog::getOpenFileNames( this, "Select files to add", "/home/jdr/mp3", "Images (*.mp3 *.wav)"); //Append selected file for (int i=0;i<newFileList.size();++i){ if(!songFilelist.contains(newFileList[i])){ //Prevent duplicate additions songFilelist.append(newFileList[i]); songList->addItem(QFileInfo(newFileList[i]).fileName()); } } } void MainWindow::favorite_clicked() { if(favorite_flag == false){ favorite_button->setIcon(QIcon(":/images/btn_favorite_yes.png")); favorite_flag = true; }else { favorite_button->setIcon(QIcon(":/images/btn_favorite_no.png")); favorite_flag = false; } } void MainWindow::listcircle_clicked() { if(circle_play == true){ listcircle_button->setIcon(QIcon(":/images/random.png")); circle_play = false; }else { listcircle_button->setIcon(QIcon(":/images/btn_listcircle.png")); circle_play = true; } } void MainWindow::menu_clicked() { if(menu_flag == false){ songList->setVisible(true); addfile_button->setVisible(true); menu_flag = true; }else { songList->setVisible(false); addfile_button->setVisible(false); menu_flag = false; } } void MainWindow::cd_timeout() { i++; if(i == 9) i=1; QString cd = QString("border-image: url(:/images/cd%1.png);").arg(i); label_cd->setStyleSheet(cd); /* * Originally, I wanted to rotate in the following way. There will be problems with the picture. I'll study it later if(i > 360){ i=0; } i +=10; QMatrix matrix; matrix.rotate(i); label_cd->setPixmap(QPixmap(":/images/cd.png").transformed(matrix, Qt::SmoothTransformation)); */ }
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile file(":/style.qss"); if (file.exists() ) { /* Open as read-only */ file.open(QFile::ReadOnly); /* Save the read result as a string */ QString styleSheet = QLatin1String(file.readAll()); /* Set global style */ qApp->setStyleSheet(styleSheet); /* Close file */ file.close(); } MainWindow w; w.show(); return a.exec(); }
style.qss
QWidget { background: "#25242a" } QListWidget{ background-color: rgba(255, 255, 255, 10%); outline:none;border-image:transparent } QListWidget::item:selected:active{ background: rgb(0, 106, 181); } QListWidget::item:hover:active{ background: qlineargradient(spread:repeat, x2:0, y2:1, x2:1, y2:0, stop:0 #a1a112, stop:0.25 #a1a112, stop:0.5 #ba89dc, stop:1 #ba89dc); }
function: