简单实现视频播放器功能

主要思路

       1.找到视频文件

       2.将视频文件名保存在一个链表中,以便切换

       3,利用已经写好的mplayer将查找到的视频文件播放出来

       4,交叉编译后在arm开发板上实现视频播放

总结:代码的编写一定要用心,打错一字母或者用错一个函数,当编译器不报错时我将可能会花一下午甚至更长的时间来找错。

思路一定要清晰,不然会花很多时间来改写代码。

      以下代码没有只能通过键盘来实现播放操作,明天将利用线程来实现对播放的快进,暂停,切换等的操作

今日代码:

/*************************************************************************
    > File Name: armplayer.c
    > Author: csgec
    > Mail: [email protected] 
    > Created Time: Wed 25 Jul 2018 09:36:33 AM HKT
 ************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<dirent.h>
#include<error.h>
#include<string.h>
#include<sys/stat.h>

typedef struct node
{
    char name[256];
    struct node *next;
    struct node *pre;
}Node;

Node *head,*tail,*p;
Node * find(char *pathname)//找文件名,并存入链表中
{
    printf("进入了查找函数&&&&&&\n");

    DIR *dir;
    char filename[512];

    dir = opendir(pathname);
    if(dir == NULL)
    {
        perror("打开路径失败\n");
        return ;
    }

    struct dirent *dirp = NULL;
    while(dirp = readdir(dir))
    {
        //当前目录和上级目录就跳过
        if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)
        {
            continue;
        }

        sprintf(filename,"%s/%s",pathname,dirp->d_name);    
        if(strcmp(filename+strlen(filename)-4,".mp4")==0 || strcmp(filename+strlen(filename)-4,".mp3")==0)
        {
            Node *p = (Node *)malloc(sizeof(Node));
            strcpy(p->name,filename);
            printf("找到的文件名 %s\n",p->name);
            p->next = p->pre = NULL;
            if(head == NULL)
            {
                head = tail = p;
            }
            else
            {
                tail->next = p;
                p->pre = tail;
                tail = p;
            }
        }
        struct stat st;//申明stat结构体
        stat(filename,&st);//将文件名放入结构体中
        if(S_ISDIR(st.st_mode))//判断是不是目录
        {
            find(filename);//是就用递归来循环查找
        }
    }
    closedir(dir);
    return head;
}

pid_t pid;
int pipefd[2];
int wfd,rfd;
char buf[128];

Node *player(char *name)//播放函數
{

    printf("进入了播放函数\n");
    printf("播放的文件名为 %s\n",name);

    mkfifo("fifo",0644);//有名管道的创建
    pid = fork();

    if(pid < 0)
    {
        perror("fork fail\n");//
        exit(-1);
    }
    if(pipe(pipefd) < 0)//无名管道的创建以及判断
    {
        perror("pipe fail\n");
    }

    if(pid == 0)//子进程
    {
        close(pipefd[0]);//关闭读空间 
        rfd = open("fifo",O_RDONLY);//只读打开有名管道
        if(rfd < 0)
        {
            perror("child open fifo fail\n");
            exit(-1);
        }
        execlp("mplayer","","-ac","mad","-slave","-quiet","-input","file=fifo",name,NULL);
    }
    else if(pid > 0)//父进程
    {
        wfd = open("fifo",O_WRONLY);//只写打开有名管道
        if(wfd < 0)
        {
            perror("parent open fifo fail\n");
            waitpid(pid,NULL,0);
            exit(-1);
        }
        close(pipefd[1]);//关闭写空间
        while(1)
        {
            char ch;
            ch = getchar();
            if(ch == 'q')
                break;
            switch(ch)
            {
                case 'p':
                    write(wfd,"pause\n",strlen("pause\n"));
                    break;
                case 'k':
                    write(wfd,"seek 3\n",strlen("seek 3\n"));
                    break;
                case 'g':
                    write(wfd,"get_time_pos\n",strlen("get_time_pos\n"));
                    memset(buf,0,128);
                    read(pipefd[0],buf,128);
                    printf("read return;%s\n",buf);
                    break;
            }
        }
    }
    kill(pid);
    waitpid(pid,NULL,0);
    close(pipefd[0]);
}


int main(int argc,char *argv[])
{
    Node *tail,*head,*p;
    head = find(argv[1]);//找到文件名
    p = head;
    player(p->name);//播放;W
    printf("end   end   end \n");
    return 0;    
}
 

猜你喜欢

转载自blog.csdn.net/qq_37589838/article/details/81266719