/* MP3音乐处理线程 */ void MP3_handleThread(void *inContext) { SD_sizeInfo sdSizeInfo; u32 ret = 0; u8 *pBuf = NULL; /* 检测是否插上SD卡 */ if (MP3_sdCardDetect() == 0) { while(1) { printf( "sdCard not insert !!!!!\r\n"); mico_thread_sleep( 1 ); } } /* MP3初始化 */ if (MP3_init() == 0) { while(1) { printf( "mp3 init failed !!!!!\r\n"); mico_thread_sleep( 1 ); } } /* 获取SD卡容量信息 */ MP3_getSdSize(&sdSizeInfo); printf("%d MB total drive space.\r\n" "%d MB available.\r\n", sdSizeInfo.totalSize, sdSizeInfo.availableSize); /* 获取MP3文件总数 */ ret = MP3_getMp3FileNum("0:/MUSIC"); printf("mp3FileNum=%d\r\n", ret); #if 0 /* 写文件 */ pBuf = malloc(512); if (pBuf != NULL) { memset(pBuf, 0x5a, 512); ret = MP3_writeSong("0:/MUSIC/hujg.mp3", 0, pBuf, 512); printf("ret0=%d\r\n", ret); memset(pBuf, 0xa5, 512); ret = MP3_writeSong("0:/MUSIC/hujg.mp3", 512, pBuf, 512); printf("ret1=%d\r\n", ret); free(pBuf); } /* 删除文件 */ ret = MP3_removeSong("0:/MUSIC/hujg_bak.mp3"); if (ret == 1) { printf("remove mp3 file success!\r\n"); } else { printf("remove mp3 file failed !\r\n"); } #endif /* 播放指定歌曲和音量设置 */ while(mico_rtos_get_semaphore(&cupTimeObj.playMp3_sem, MICO_WAIT_FOREVER) == kNoErr) { if (cupTimeObj.playMode != PLAY_ONLY_LED) //如果不是单单灯提醒喝水时 { MP3_setVolume(20); printf("MP3_getVolume=%d\r\n", MP3_getVolume()); ret = MP3_playSong("0:/MUSIC/水晶闹钟.mp3"); //播放指定歌曲 printf("play over!!!!ret=%d\r\n", ret); mico_rtos_set_semaphore(&cupTimeObj.stopLed_sem); //通知停止亮LED灯 } // mp3_play(); } }
//播放音乐 void mp3_play(void) { u8 res; DIR mp3dir; //目录 FILINFO mp3fileinfo;//文件信息 u8 *fn; //长文件名 u8 *pname; //带路径的文件名 u16 totmp3num; //音乐文件总数 u16 curindex; //当前索引 u8 key; //键值 u16 temp; u16 *mp3indextbl; //音乐索引表 while(f_opendir(&mp3dir,"0:/MUSIC"))//打开音乐文件夹 { printf("MUSIC文件夹错误!\r\n"); mico_thread_msleep(200); } totmp3num=mp3_get_tnum("0:/MUSIC"); //得到总有效文件数 while(totmp3num==NULL)//音乐文件总数为0 { printf("没有音乐文件!\r\n"); mico_thread_msleep(200); } mp3fileinfo.lfsize = _MAX_LFN*2+1; //长文件名最大长度 mp3fileinfo.lfname = malloc(mp3fileinfo.lfsize);//为长文件缓存区分配内存 pname = malloc(mp3fileinfo.lfsize); //为带路径的文件名分配内存 mp3indextbl = malloc(2*totmp3num); //申请2*totmp3num个字节的内存,用于存放音乐文件索引 while(mp3fileinfo.lfname==NULL||pname==NULL||mp3indextbl==NULL)//内存分配出错 { printf("内存分配失败!\r\n"); mico_thread_msleep(200); } //记录索引 res=f_opendir(&mp3dir,"0:/MUSIC"); //打开目录 if(res==FR_OK) { curindex=0;//当前索引为0 while(1)//全部查询一遍 { temp=mp3dir.index; //记录当前index res=f_readdir(&mp3dir,&mp3fileinfo); //读取目录下的一个文件 if(res!=FR_OK||mp3fileinfo.fname[0]==0)break; //错误了/到末尾了,退出 fn=(u8*)(*mp3fileinfo.lfname?mp3fileinfo.lfname:mp3fileinfo.fname); res=f_typetell(fn); if((res&0XF0)==0X40)//取高四位,看看是不是音乐文件 { mp3indextbl[curindex]=temp;//记录索引 curindex++; } } } curindex=0; //从0开始显示 res=f_opendir(&mp3dir,(const TCHAR*)"0:/MUSIC"); //打开目录 while(res==FR_OK)//打开成功 { dir_sdi(&mp3dir,mp3indextbl[curindex]); //改变当前目录索引 res=f_readdir(&mp3dir,&mp3fileinfo); //读取目录下的一个文件 if(res!=FR_OK||mp3fileinfo.fname[0]==0)break; //错误了/到末尾了,退出 fn=(u8*)(*mp3fileinfo.lfname?mp3fileinfo.lfname:mp3fileinfo.fname); strcpy((char*)pname,"0:/MUSIC/"); //复制路径(目录) strcat((char*)pname,(const char*)fn); //将文件名接在后面 printf("歌曲名字: %s\r\n", fn); //显示歌曲名字 mp3_index_show(curindex+1,totmp3num); key=MP3_playSong(pname); //播放这个MP3 if(key==KEY0_PRES)//下一曲 { curindex++; if(curindex>=totmp3num)curindex=0;//到末尾的时候,自动从头开始 }else break; //产生了错误 } free(mp3fileinfo.lfname); //释放内存 free(pname); //释放内存 free(mp3indextbl); //释放内存 }