聚豐項(xiàng)目 > 即時(shí)播放視頻
即時(shí)播放視頻,主要是用于通過(guò)WiFi傳輸音頻和視頻文件到設(shè)備中即時(shí)播放,并不受限于音頻,視頻文件的大小。也不需要設(shè)備有很大的一個(gè)存儲(chǔ)空間。采用邊播邊放邊刪的一個(gè)方式,在設(shè)備中每次只存儲(chǔ)一部分要播放的文件,播過(guò)的文件即時(shí)刪掉,因此只需一個(gè)很小的存儲(chǔ)空間就可以播放一個(gè)很大的音頻視頻文件。
diney_db4
分享diney_db4
團(tuán)隊(duì)成員
張文雄 程序員
開(kāi)發(fā)板:NUCLEO-64 STM32F401
主控MCU:STM32F401RET6
WIFI模塊:3080B
音頻解碼模塊:VS1053
液晶顯示:TFTLCD液晶屏
下位機(jī)即時(shí)播放音頻,視頻部分代碼。
void mp3_play(void)
{
u8 res;
DIR mp3dir; //????
FILINFO mp3fileinfo;//???tD??¢
u8 *fn; //3¤???t??
u8 *pname; //′??·??μ????t??
u16 totmp3num; //ò?à????t×üêy
u16 curindex; //í???μ±?°?÷òy
u8 key; //?ü?μ
u16 temp;
u16 *mp3indextbl; //ò?à??÷òy±í
while(f_opendir(&mp3dir,"0:/MUSIC"))//′ò?aí??????t?D
{
Show_Str(30,190,240,16,"MUSIC???t?D′í?ó!",16,0);
delay_ms(200);
LCD_Fill(30,190,240,206,WHITE);//??3y??ê?
delay_ms(200);
}
totmp3num=mp3_get_tnum("0:/MUSIC"); //μ?μ?×üóDD§???têy
while(totmp3num==NULL)//ò?à????t×üêy?a0
{
Show_Str(30,190,240,16,"??óDò?à????t!",16,0);
delay_ms(200);
LCD_Fill(30,190,240,146,WHITE);//??3y??ê?
delay_ms(200);
}
mp3fileinfo.lfsize=_MAX_LFN*2+1; //3¤???t??×?′ó3¤?è
mp3fileinfo.lfname=mymalloc(SRAMIN,mp3fileinfo.lfsize); //?a3¤???t?o′???·????ú′?
pname=mymalloc(SRAMIN,mp3fileinfo.lfsize); //?a′??·??μ????t??·????ú′?
mp3indextbl=mymalloc(SRAMIN,2*totmp3num); //éê??2*totmp3num??×??úμ??ú′?,ó?óú′?·?ò?à????t?÷òy
while(mp3fileinfo.lfname==NULL||pname==NULL||mp3indextbl==NULL)//?ú′?·???3?′í
{
Show_Str(30,190,240,16,"?ú′?·???꧰ü!",16,0);
delay_ms(200);
LCD_Fill(30,190,240,146,WHITE);//??3y??ê?
delay_ms(200);
}
VS_HD_Reset();
VS_Soft_Reset();
vsset.mvol=220; //??è?éè??ò?á??a220.
mp3_vol_show((vsset.mvol-100)/5); //ò?á??T???ú:100~250,??ê?μ?ê±oò,°′??1?ê?(vol-100)/5,??ê?,ò2?íê?0~30
//?????÷òy
res=f_opendir(&mp3dir,"0:/MUSIC"); //′ò?a????
if(res==FR_OK)
{
curindex=0;//μ±?°?÷òy?a0
while(1)//è?2?2é?ˉò?±é
{
temp=mp3dir.index; //????μ±?°index
res=f_readdir(&mp3dir,&mp3fileinfo); //?áè???????μ?ò??????t
if(res!=FR_OK||mp3fileinfo.fname[0]==0)break; //′í?óá?/μ????2á?,í?3?
fn=(u8*)(*mp3fileinfo.lfname?mp3fileinfo.lfname:mp3fileinfo.fname);
res=f_typetell(fn);
if((res&0XF0)==0X40)//è???????,?′?′ê?2?ê?ò?à????t
{
mp3indextbl[curindex]=temp;//?????÷òy
curindex++;
}
}
}
curindex=0; //′ó0?aê???ê?
res=f_opendir(&mp3dir,(const TCHAR*)"0:/MUSIC"); //′ò?a????
while(res==FR_OK)//′ò?a3é1|
{
dir_sdi(&mp3dir,mp3indextbl[curindex]); //??±?μ±?°?????÷òy
res=f_readdir(&mp3dir,&mp3fileinfo); //?áè???????μ?ò??????t
if(res!=FR_OK||mp3fileinfo.fname[0]==0)break; //′í?óá?/μ????2á?,í?3?
fn=(u8*)(*mp3fileinfo.lfname?mp3fileinfo.lfname:mp3fileinfo.fname);
strcpy((char*)pname,"0:/MUSIC/"); //?′???·??(????)
strcat((char*)pname,(const char*)fn); //?????t???ó?úoó??
LCD_Fill(30,190,240,190+16,WHITE); //??3y???°μ???ê?
Show_Str(30,190,240-30,16,fn,16,0); //??ê??è?ú??×?
mp3_index_show(curindex+1,totmp3num);
key=mp3_play_song(pname); //2¥·??a??MP3
if(key==2) //é?ò??ú
{
if(curindex)curindex--;
else curindex=totmp3num-1;
}else if(key<=1)//??ò??ú
{
curindex++;
if(curindex>=totmp3num)curindex=0;//μ????2μ?ê±oò,×??ˉ′óí·?aê?
}else break; //2úéúá?′í?ó
}
myfree(SRAMIN,mp3fileinfo.lfname); //êí·??ú′?
myfree(SRAMIN,pname); //êí·??ú′?
myfree(SRAMIN,mp3indextbl); //êí·??ú′?
}
unsigned char PlayMusicStreamFunction(unsigned char *dataBuffer, unsigned short lenght)
{
PlayMusicStream = 4;
if(MusicStreamDataCount>100)
{
PlayMusicEnFlag = 1;
MusicStreamDataRecciveCount++;
}
//#if PRINTF_DEBUG == 1
//printf(" ???? MusicStreamDataCount %d , Mp3FlieSizeCount %d ?? %d \r\n",MusicStreamDataCount, Mp3FlieSizeCount,GetSystick1ms());
///#endif
if(PLAY_MUSIC_LENGHT - MusicStreamDataCount >= lenght)
{
memcpy(PlayMusicData+MusicStreamDataCount,dataBuffer,lenght);
MusicStreamDataCount += lenght;
}
else
{
MusicStreamDataCount = 0;
memcpy(PlayMusicData+MusicStreamDataCount,dataBuffer,lenght);
MusicStreamDataCount += lenght;
PlayMusicFlay = 1;
}
if(Mp3FlieSizeCount > MusicStreamDataCount)
{
if((Mp3FlieSizeCount - MusicStreamDataCount) < 4*1024)
return 0;
}
else if(PlayMusicFlay)
{
if((MusicStreamDataCount - Mp3FlieSizeCount) < 4*1024)
return 0;
}
return 1;
}
void SystemProcessPlayMusic(void)
{
//static unsigned int size = 0;
unsigned int lenght = 0;
if(PlayMusicEnFlag)
{
if(StartGetMusicData)
{
StartGetMusicData = 0;
if(PlayMusicStream>3)
{
if(Mp3FlieSizeCount < PLAY_MUSIC_LENGHT)
{
if(MusicStreamDataRecciveCount)
{
MusicStreamDataRecciveCount--;
}
if(StopPlayRobotMusic)
{
StopPlayRobotMusic = 0;
Mp3FlieSizeCount = 0;
PlayMusicEnFlag = 0;
//StopSkillPlay();
//memset(Mp3FileName,0,50);
//memset(PlayMusicData,0,1024);
//f_close(&Mp3_fnew);
#if PRINTF_DEBUG == 1
printf("StopPlayRobotMusic í£?12¥·? \r\n");
#endif
return;
}
lenght = 1024;
//#if PRINTF_DEBUG_fatfs == 1
//printf(" ?y?ú2¥·? ±?2¥±?·? MusicStreamDataCount %d £? Mp3FlieSizeCount %d ê±?? %d \r\n",MusicStreamDataCount, Mp3FlieSizeCount,GetSystick1ms());
//#endif
System.Device.Music.PlayMp3File(PlayMusicData+Mp3FlieSizeCount,lenght);
Mp3FlieSizeCount += lenght;
if(Mp3FlieSizeCount == MusicStreamDataCount)
{
PlayMusicStream = 1;
Mp3FlieSizeCount = 0;
MusicStreamDataCount = 0;
PlayMusicFlay = 0;
}
if(Mp3FlieSizeCount >= PLAY_MUSIC_LENGHT)
{
Mp3FlieSizeCount = 0;
}
}
}
else if(PlayMusicStream == 0)
{
if(Mp3FlieSizeCount < Mp3_fnew.fsize)
{
f_lseek(&Mp3_fnew, Mp3FlieSizeCount);
lenght = Mp3_fnew.fsize - Mp3FlieSizeCount;
if(lenght > MAX_BYTECOUNT)
lenght = MAX_BYTECOUNT;
Mp3_res_sd = f_read(&Mp3_fnew, PlayMusicData, lenght, &Mp3_fnum);
if(Mp3_res_sd == FR_OK)
{
play_music((unsigned char*)(&PlayMusicData[0]),lenght);
Mp3FlieSizeCount += lenght;
if(Mp3FlieSizeCount >= Mp3_fnew.fsize)
{
Mp3FlieSizeCount = 0;
PlayMusicEnFlag = 0;
f_close(&Mp3_fnew);
}
}
else
{
Mp3FlieSizeCount = 0;
PlayMusicEnFlag = 0;
f_close(&Mp3_fnew);
}
#if PRINTF_DEBUG == 1
printf("2¥·?mp3???t count %ld \r\n",Mp3FlieSizeCount);
#endif
}
else
{
Mp3FlieSizeCount = 0;
PlayMusicEnFlag = 0;
f_close(&Mp3_fnew);
}
}
}
}
}