2023-08-29 17:08:51 +08:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
|
|
|
: QMainWindow(parent)
|
|
|
|
, ui(new Ui::MainWindow)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-08-29 20:41:42 +08:00
|
|
|
initMainWindow(); // 初始化主界面
|
2023-08-30 16:09:27 +08:00
|
|
|
mplayerInit(); // 初始化 mplayer
|
2023-08-29 17:08:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-08-29 20:24:16 +08:00
|
|
|
void MainWindow::initMainWindow()
|
|
|
|
{
|
2023-08-29 20:41:42 +08:00
|
|
|
// 设置主窗体属性
|
2023-08-29 20:24:16 +08:00
|
|
|
setWindowTitle("音乐播放器");
|
2023-08-29 20:41:42 +08:00
|
|
|
|
|
|
|
// 设置子组件背景透明
|
2023-08-29 20:24:16 +08:00
|
|
|
ui->song_list_widget->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->label_artist->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->label_song_list->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->label_song_name->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->label_meta_album->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->contrl_widget->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->widget_show->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->volume_widget->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->widget_3->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->time_left_label->setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
ui->time_right_label->setAttribute(Qt::WA_TranslucentBackground);
|
2023-08-29 20:41:42 +08:00
|
|
|
ui->time_widget->setAttribute(Qt::WA_TranslucentBackground);
|
2023-08-29 20:24:16 +08:00
|
|
|
|
2023-08-29 20:41:42 +08:00
|
|
|
// 设置滑块属性
|
2023-08-29 20:24:16 +08:00
|
|
|
ui->time_slider->setStyleSheet("QSlider::groove:horizontal { background-color: white; }"
|
|
|
|
"QSlider::handle:horizontal { background-color: blue; width: 20px;}");
|
|
|
|
ui->volume_slider->setStyleSheet("QSlider::groove:horizontal { background-color: gray; }"
|
|
|
|
"QSlider::handle:horizontal { background-color: yellow; width: 20px;}");
|
2023-08-29 20:41:42 +08:00
|
|
|
ui->volume_slider->setMinimum(0); // 最小音量 0
|
|
|
|
ui->volume_slider->setMaximum(100); // 最大音量 100
|
|
|
|
// 当前音量从 mplayer 属性获取
|
|
|
|
|
|
|
|
// QSlider* slider = new QSlider(Qt::Horizontal);
|
|
|
|
// slider->setMinimum(0); // 设置最小值
|
|
|
|
// slider->setMaximum(100); // 设置最大值
|
|
|
|
// slider->setValue(50); // 设置当前值
|
|
|
|
// slider->setSingleStep(1); // 设置步长
|
|
|
|
|
2023-08-29 20:24:16 +08:00
|
|
|
}
|
2023-08-29 21:14:28 +08:00
|
|
|
|
|
|
|
void MainWindow::mplayerInit()
|
|
|
|
{
|
|
|
|
// 创建一个无名管道
|
|
|
|
int fd[2];
|
|
|
|
pipe(fd);
|
2023-08-30 16:09:27 +08:00
|
|
|
|
|
|
|
// 创建有名管道
|
|
|
|
mkfifo("fifo_cmd",0666);
|
|
|
|
|
|
|
|
// 创建 子进程启动 mplayer
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == 0) // 子进程
|
|
|
|
{
|
|
|
|
// 重定向标准输出
|
|
|
|
dup2(fd[1],1);
|
|
|
|
// 使用 exec 启动 mplayer
|
|
|
|
execlp("mplayer","mplayer","-idle","-slave","-quiet","-input","file=fifo_cmd","./song/01.mp3",NULL);
|
|
|
|
_exit(-1); // 异常退出
|
|
|
|
}
|
|
|
|
else if(pid>0) // 父进程
|
|
|
|
{
|
|
|
|
// 打开管道
|
|
|
|
// 以写的方式打开(阻塞到某进程 以读的方式打开)
|
|
|
|
fifo_fd = open("fifo_cmd",O_WRONLY);
|
|
|
|
if(fifo_fd < 0){
|
|
|
|
perror("open");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 21:14:28 +08:00
|
|
|
}
|