temp modify
This commit is contained in:
parent
0fe9ca51a1
commit
9e8db65509
63
main.cpp
63
main.cpp
|
@ -2,10 +2,73 @@
|
|||
|
||||
#include <QApplication>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
|
||||
void *getTimerThreadFunc(void *arg); // 发送获取当前时间的 mplayer 命令
|
||||
void *readPipeMSG(void *arg); // 获取当前时间的线程
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
// pthread_t t1,t2; //
|
||||
// pthread_create(&t1,NULL,getTimerThreadFunc,(void*)&w);
|
||||
// pthread_create(&t2,NULL,readPipeMSG,(void*)&w);
|
||||
// pthread_join(t1,NULL);
|
||||
// pthread_join(t2,NULL);
|
||||
|
||||
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
void *getTimerThreadFunc(void *arg)
|
||||
{
|
||||
MainWindow *p = (MainWindow *)arg;
|
||||
|
||||
while (1)
|
||||
{
|
||||
// 发送获取的当前时间的命令
|
||||
// 发送 get_time_pos 命令到管道中
|
||||
write(p->fifo_fd, "get_time_pos\n", strlen("get_time_pos\n"));
|
||||
usleep(500*1000); // 休眠 0.5 秒
|
||||
|
||||
// 发送获取当前歌曲进度的命令
|
||||
write(p->fifo_fd,"get_percent_pos\n",strlen("get_percent_pos\n"));
|
||||
usleep(500*1000); // 休眠 0.5 秒
|
||||
}
|
||||
}
|
||||
|
||||
void *readPipeMSG(void *arg)
|
||||
{
|
||||
MainWindow *p = (MainWindow *)arg;
|
||||
|
||||
// 不停的获取无名管道的数据
|
||||
while (1)
|
||||
{
|
||||
char buf[256] = "";
|
||||
int ret = read(p->fd[0], buf, sizeof(buf)); // 从无名管道读取内容到 buf
|
||||
if(ret > 0)
|
||||
{
|
||||
// 判断消息的类型 :
|
||||
// 如果消息是 "当前歌曲播放时间位置"
|
||||
if(strncmp(buf,"ANS_TIME_POSITION",strlen("ANS_TIME_POSITION")) == 0)
|
||||
{
|
||||
int time = 0;
|
||||
sscanf(buf,"ANS_TIME_POSITION=%d",&time); // 从数据流 buf 中读取时间到 time
|
||||
// 给 UI 发送当前时间信号
|
||||
emit p->currentTimeSignal(time);
|
||||
}
|
||||
// 如果消息是 "当前歌曲播放进度"
|
||||
else if(strncmp(buf,"ANS_PERCENT_POSITION", strlen("ANS_PERCENT_POSITION")) == 0)
|
||||
{
|
||||
int currentProgress = 0;
|
||||
sscanf(buf, "ANS_PERCENT_POSITION=%d", ¤tProgress);
|
||||
// 给 UI 发送进度信号
|
||||
emit p->currentProgressSignal(currentProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow)
|
||||
|
@ -8,6 +9,9 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
ui->setupUi(this);
|
||||
initMainWindow(); // 初始化主界面
|
||||
mplayerInit(); // 初始化 mplayer
|
||||
scanSong(); //
|
||||
|
||||
|
||||
|
||||
// 捕获当前时间信号
|
||||
connect(this, SIGNAL(currentTimeSignal(int)), this, SLOT(setCurrentTimeLabel(int)));
|
||||
|
@ -68,11 +72,14 @@ void MainWindow::mplayerInit()
|
|||
pid_t pid = fork();
|
||||
if(pid == 0) // 子进程
|
||||
{
|
||||
|
||||
|
||||
|
||||
// 重定向标准输出
|
||||
dup2(fd[1], 1);
|
||||
// 使用 exec 启动 mplayer
|
||||
// execlp("mplayer","mplayer","-idle","-slave","-quiet","/home/flykhan/qtmplayer/song/StopLove.mp3",NULL);
|
||||
execlp("mplayer","mplayer","-idle","-slave","-quiet","-input","file=fifo_cmd","/home/flykhan/qtmplayer/song/StopLove.mp3",NULL);
|
||||
execlp("/usr/bin/mplayer","mplayer","-idle","-slave","-quiet","-input","file=fifo_cmd","/home/flykhan/qtmplayer/song/StopLove.mp3",NULL);
|
||||
_exit(-1); // 异常退出
|
||||
}
|
||||
else if(pid>0) // 父进程
|
||||
|
@ -91,7 +98,7 @@ void MainWindow::mplayerInit()
|
|||
void MainWindow::scanSong()
|
||||
{
|
||||
// 1. 得到目录句柄
|
||||
DIR *dir = opendir("./song");
|
||||
DIR *dir = opendir("/home/flykhan/qtmplayer/song");
|
||||
if(NULL == dir)
|
||||
{
|
||||
perror("opendir");
|
||||
|
@ -130,57 +137,11 @@ void MainWindow::playNextSong(void)
|
|||
// 将 loadfile 'song_name_path' 写入字符数组
|
||||
int len = sprintf(changeSongBuf, "loadfile ./song/%s\n", vectorSong[index].c_str());
|
||||
qDebug() << changeSongBuf << endl; // 控制台打印下一首歌的名字
|
||||
write(this->fifo_fd, changeSongBuf, len); // 将获取的下一首歌曲名写入到有名管道中
|
||||
write(this->fifo_fd, changeSongBuf.toUtf8().toStdString().c_str(),changeSongBuf.toUtf8().toStdString().size());
|
||||
// write(this->fifo_fd, changeSongBuf, len); // 将获取的下一首歌曲名写入到有名管道中
|
||||
}
|
||||
|
||||
void *MainWindow::getTimerThreadFunc(void *arg)
|
||||
{
|
||||
MainWindow *p = (MainWindow *)arg;
|
||||
|
||||
while (1)
|
||||
{
|
||||
// 发送获取的当前时间的命令
|
||||
// 发送 get_time_pos 命令到管道中
|
||||
write(p->fifo_fd, "get_time_pos\n", strlen("get_time_pos\n"));
|
||||
usleep(500*1000); // 休眠 0.5 秒
|
||||
|
||||
// 发送获取当前歌曲进度的命令
|
||||
write(p->fifo_fd,"get_percent_pos\n",strlen("get_percent_pos\n"));
|
||||
usleep(500*1000); // 休眠 0.5 秒
|
||||
}
|
||||
}
|
||||
|
||||
void *MainWindow::readPipeMSG(void *arg)
|
||||
{
|
||||
MainWindow *p = (MainWindow *)arg;
|
||||
|
||||
// 不停的获取无名管道的数据
|
||||
while (1)
|
||||
{
|
||||
char buf[256] = "";
|
||||
int ret = read(p->fd[0], buf, sizeof(buf)); // 从无名管道读取内容到 buf
|
||||
if(ret > 0)
|
||||
{
|
||||
// 判断消息的类型 :
|
||||
// 如果消息是 "当前歌曲播放时间位置"
|
||||
if(strncmp(buf,"ANS_TIME_POSITION",strlen("ANS_TIME_POSITION")) == 0)
|
||||
{
|
||||
int time = 0;
|
||||
sscanf(buf,"ANS_TIME_POSITION=%d",&time); // 从数据流 buf 中读取时间到 time
|
||||
// 给 UI 发送当前时间信号
|
||||
emit p->currentTimeSignal(time);
|
||||
}
|
||||
// 如果消息是 "当前歌曲播放进度"
|
||||
else if(strncmp(buf,"ANS_PERCENT_POSITION", strlen("ANS_PERCENT_POSITION")) == 0)
|
||||
{
|
||||
int currentProgress = 0;
|
||||
sscanf(buf, "ANS_PERCENT_POSITION=%d", ¤tProgress);
|
||||
// 给 UI 发送进度信号
|
||||
emit p->currentProgressSignal(currentProgress);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::setCurrentTimeLabel(int time)
|
||||
{
|
||||
|
@ -201,5 +162,20 @@ void MainWindow::setCurrentSlider(int currentProgress)
|
|||
|
||||
void MainWindow::on_play_btn_clicked()
|
||||
{
|
||||
ui->play_btn->setIcon(QIcon(":/icon/button"));
|
||||
if(play_btn_flag == 0)
|
||||
{
|
||||
ui->play_btn->setIcon(QIcon(":/icon/button"));
|
||||
|
||||
play_btn_flag = 1;
|
||||
}
|
||||
else if (play_btn_flag == 1)
|
||||
{
|
||||
ui->play_btn->setIcon(QIcon(":/icon/button_play"));
|
||||
play_btn_flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::setSongList()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
10
mainwindow.h
10
mainwindow.h
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QMainWindow>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
@ -24,12 +25,15 @@ class MainWindow : public QMainWindow
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
public:
|
||||
int fd[2]; // 声明无名管道
|
||||
int fifo_fd; // 声明有名管道
|
||||
int index = 0; // 歌曲索引
|
||||
vector<string> vectorSong; // 歌曲名动态数组
|
||||
|
||||
|
||||
int play_btn_flag = 0; // play btn flag
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
@ -38,8 +42,8 @@ public:
|
|||
void initMainWindow(); // 主窗体初始化
|
||||
void mplayerInit(); // 初始化 mplayer
|
||||
void scanSong(); // 扫描歌曲文件
|
||||
void *getTimerThreadFunc(void *arg); // 发送获取当前时间的 mplayer 命令
|
||||
void *readPipeMSG(void *arg); // 获取当前时间的线程
|
||||
void setSongList();
|
||||
|
||||
|
||||
|
||||
public slots:
|
||||
|
|
Loading…
Reference in New Issue