#include "do.h"
#include "show.h"
int FileStat = 0; // 切换媒体文件字符串数组
STATING stat_get = STAT_FREE;
VIDEO_MOD MODING = CYCLE_ORDER;
extern char *filename[1024];
extern int filenum;
void GetVideoFile(void)
{
DIR *fdir;
fdir = opendir("/home/linux/Music");
if (NULL == fdir)
{
perror("fail to open");
exit(0);
}
struct dirent *pdir;
while (1)
{
pdir = readdir(fdir);
if (NULL == pdir)
break;
char *tmpbuf = pdir->d_name;
while ('\0' != *tmpbuf) // 找末尾
{
tmpbuf++;
}
while ('.' != *tmpbuf) // 找 .
{
tmpbuf--;
}
if (!strcmp(tmpbuf, ".mp3") || !strcmp(tmpbuf, ".mp4") || !strcmp(tmpbuf, ".avi") || !strcmp(tmpbuf, ".wma") || !strcmp(tmpbuf, ".rmvb") || !strcmp(tmpbuf, ".flv") || !strcmp(tmpbuf, ".rm"))
{
filename[filenum++] = pdir->d_name;
}
}
}
int GetChoose(void) // 接收选项
{
char tmp[4];
fgets(tmp, 4, stdin);
return atoi(tmp);
}
void Start_Pause(void) // 开始、暂停
{
if (stat_get == STAT_FREE) //==========开始=========
{
char tmpbuf[1024] = {0};
sprintf(tmpbuf, "/home/linux/Music/%s", filename[FileStat]);
stat_get = STAT_ON;
pid_t pid = fork();
if (pid > 0)
{
// 父进程
}
else if (pid == 0)
{
close(1);
close(2);
execlp("mplayer", "mplayer", "-slave", "-input", "file=./fifo_mp3", tmpbuf, NULL);
execlp("mplayer", "mplayer", "-slave", "-input", "file=./fifo_mp3a", tmpbuf, NULL);
perror("fail to mplayer");
}
else
{
perror("fail to fork");
return;
}
}
else //=============暂停pause============
{
int fd = open("./fifo_mp3", O_WRONLY);
if (-1 == fd)
{
perror("fail to open");
return;
}
write(fd, "pause\n", 6);
if (stat_get == STAT_ON)
{
stat_get = STAT_PAUSE;
}
else if (stat_get == STAT_PAUSE)
{
stat_get = STAT_ON;
}
close(fd);
}
}
void StopOff(void) // 停止
{
int fd = open("./fifo_mp3", O_WRONLY | O_NONBLOCK);
if (-1 == fd)
{
perror("fail to open");
return;
}
write(fd, "stop\n", 5);
close(fd);
stat_get = STAT_FREE;
}
void LastFile(void) // 上一个
{
if (MODING == CYCLE_ORDER) // 顺序循环
{
if (0 == FileStat)
{
printf("已是第一个文件无法切换上一个\n");
sleep(2);
return;
}
StopOff(); // 关闭当前文件
FileStat--;
Start_Pause(); // 播放
}
else if (MODING == CYCLE_SINGLE) // 单曲循环
{
StopOff(); // 关闭当前文件
Start_Pause(); // 播放
}
else if (MODING == CYCLE_RANDOM) // 随机循环
{
StopOff(); // 关闭当前文件
FileStat = rand() % filenum;
Start_Pause(); // 播放
}
}
void NextFile(void) // 下一个
{
if (MODING == CYCLE_ORDER)
{
if (filenum == (FileStat + 1))
{
printf("最后一个文件无法切换下一个\n");
sleep(2);
return;
}
StopOff(); // 关闭当前文件
FileStat++;
Start_Pause(); // 播放
}
else if (MODING == CYCLE_SINGLE) // 单曲循环
{
StopOff(); // 关闭当前文件
Start_Pause(); // 播放
}
else if (MODING == CYCLE_RANDOM) // 随机循环
{
StopOff(); // 关闭当前文件
FileStat = rand() % filenum;
Start_Pause(); // 播放
}
}
void SpeedOn(void) // 倍速播放
{
ShowSpeed();
char tmp[12];
fgets(tmp, sizeof(tmp), stdin);
if (!strcmp("last\n", tmp))
{
return;
}
int fd = open("./fifo_mp3", O_WRONLY | O_NONBLOCK); // 管道会阻塞
if (-1 == fd)
{
perror("fail to open");
sleep(2);
return;
}
int data = atoi(tmp);
switch (data)
{
case 1:
write(fd, "speed_set 1\n", 12);
printf("\33[1m当前为 1 倍速\33[0m\n");
sleep(2);
break;
case 2:
write(fd, "speed_set 2\n", 12);
printf("\33[1m当前为 2 倍速\33[0m\n");
sleep(2);
break;
case 3:
write(fd, "speed_set 4\n", 12);
printf("\33[1m当前为 4 倍速\33[0m\n");
sleep(2);
break;
default:
printf("\33[31m\33[1m输入不正确,请重新输入!!!\33[0m\n");
sleep(2);
SpeedOn();
break;
}
close(fd);
}
void Location(void) // 定位
{
ShowLocation();
char tmp[12];
fgets(tmp, sizeof(tmp), stdin);
if (!strcmp("last\n", tmp))
{
return;
}
int fd = open("./fifo_mp3", O_WRONLY | O_NONBLOCK); // 管道会阻塞
if (-1 == fd)
{
perror("fail to open");
sleep(2);
return;
}
int data = atoi(tmp);
char tmpbuf[1024] = {0};
if (data >= 0 && data <= 100)
{
sprintf(tmpbuf, "seek %d 1\n", data);
write(fd, tmpbuf, strlen(tmpbuf));
close(fd);
return;
}
printf("\33[31m\33[1m输入不正确,请重新输入!!!\33[0m\n");
sleep(2);
Location();
}
void VideoMode(void) // 播放方式
{
ShowMode();
char tmp[12];
fgets(tmp, sizeof(tmp), stdin);
if (!strcmp("last\n", tmp))
{
return;
}
int fd = open("./fifo_mp3", O_WRONLY | O_NONBLOCK); // 管道会阻塞
if (-1 == fd)
{
perror("fail to open");
sleep(2);
return;
}
int data = atoi(tmp);
switch (data)
{
case 1:
MODING = CYCLE_ORDER;
printf("\33[1m当前为循环播放\33[0m\n");
sleep(2);
break;
case 2:
MODING = CYCLE_SINGLE;
printf("\33[1m当前为单曲循环\33[0m\n");
sleep(2);
break;
case 3:
MODING = CYCLE_RANDOM;
printf("\33[1m当前为随机播放\33[0m\n");
sleep(2);
break;
default:
printf("\33[31m\33[1m输入不正确,请重新输入!!!\33[0m\n");
sleep(2);
VideoMode();
break;
}
close(fd);
}
void QuitApp(void) // 退出
{
StopOff();
printf("\33[42m\33[1m\33[31m 期待下次再见! \33[0m\n");
printf("\33[1m Mplayer已退出!!!\33[0m\n");
exit(0);
}