qfedu-c-level/lyric_analysis/srcs/start_mplayer.c

25 lines
1.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "./includes/start_mplayer.h"
//启动mplayer播放器需要安装mplayer, 安装命令sudo apt install mplayer -y
//命令测试mplayer播放器 mplayer 简单爱.mp3
//参数song_path 为歌曲的路径
void mplayer_play(const char *song_path) // 启动mplayer播放器播放歌曲
{
pid_t pid; // 进程id
pid = fork(); // 创建子进程, 用于启动mplayer播放器
if (pid < 0) // 创建失败, fork()返回值小于0表示创建子进程失败
{
perror("fork"); // 打印错误信息
}
else if (pid == 0) // 子进程中, fork()返回值为0表示当前进程为子进程
{
close(1); // 关闭标准输出
close(2); // 关闭标准错误输出
// 启动mplayer播放器参数为歌曲路径-slave表示mplayer进入slave模式-quiet表示mplayer进入安静模式不输出任何信息NULL表示参数列表结束execlp()函数执行成功后当前进程就被替换成mplayer进程所以当前进程不会执行到下面的代码
execlp("mplayer", "mplayer", "-slave", "-quiet", song_path, NULL);
// execlp("mplayer", "mplayer", "-slave", song_path, NULL);
exit(0); // 子进程退出
}
}