LPC5411x mp3制作 之遍历 SD卡内 所用MP3文件

记录一下自己编程中遇到的问题及解决的方法:

目的:遍历sd中所有的文件夹,把mp3格式的歌曲的路径提取出来。

方法:已经移植好的fatfs 嵌入式文件 系统

/*!
 * @brief Traversing files of the specified file type in the directory
 *
 * @param root directory
 * @param the type need to be find (file suffix)
 * @param the type need to be find (file suffix)
 * @param This structure is used to store the results
 * @return Return the result of the open directory
 */
char scan_files(char *filePath,char *fileSuffix1,char *fileSuffix2,Mp3File *resultFile)
{
    FRESULT res;
    DIR dir;
    static int i=0;  
    FILINFO fileinfo;
    char path[MAX_FILE_PATH_LENGTH];
    char tempPath[MAX_FILE_PATH_LENGTH];
    
    strcpy(path,filePath);
    res = f_opendir(&dir,(const TCHAR*)path);                    //open path
    if (res == FR_OK) 
     {  
        strcat(path,"/");
        while(1)
        {
            res = f_readdir(&dir, &fileinfo);                   //Read each file in the path in turn
            if (res != FR_OK || fileinfo.fname[0] == 0) break;  
            if (fileinfo.fname[0] == '.') continue;             
            if (fileinfo.fattrib & AM_DIR) {                     //Determine if it is a folder
                strcpy(tempPath,path);
                strcat(tempPath,fileinfo.fname);
                //strcat(path,fileinfo.fname);
                
                PRINTF("filedirpath=%s\n",tempPath);
                res = scan_files(tempPath,fileSuffix1,fileSuffix2,resultFile);
               if (res != FR_OK) break;
           } else {
               if(strstr(fileinfo.fname,fileSuffix1) || strstr(fileinfo.fname,fileSuffix2)) //Determine if it is the file you are looking for
               {
//                   PRINTF("current path =%s\n",path);
//                   PRINTF ("mp3 file name = %s\n",fileinfo.fname);
                   strcpy(tempPath,path);
                   strcat(tempPath,fileinfo.fname);
                   strcpy(resultFile->name[i] ,tempPath);
                   resultFile->length=i;
                   i++;
               }
           }        
        } 
        f_closedir(&dir);
    }     
 return res;      
}

调用方法如下:

typedef struct mp3file{
    char name[MAX_MP3_FILE_NUM][MAX_FILE_PATH_LENGTH];
    int length;
}Mp3File;


Mp3File mp3File;

scan_files("4:",".mp3",".MP3",&mp3File);

你可能感兴趣的:(lpc5411x,mp3音乐播放器制作)