当前位置:我的异常网» C语言 » 小弟用VC6.0跟C语言开发的播放器,但是播放没声音
小弟用VC6.0跟C语言开发的播放器,但是播放没声音(2)
www.myexceptions.net 网友分享于:2013-08-12 浏览:7次
static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = 0;
int len1,data_size;
for(;;){
while(audio_pkt_size>0){
data_size = buf_size;
len1 = avcodec_decode_audio2(aCodecCtx,(int16_t *)audio_buf,
&data_size,audio_pkt_data,audio_pkt_size);
if(len1<0){
audio_pkt_size = 0;
break;
}
audio_pkt_data += len1;
audio_pkt_size -=len1;
if(data_size<=0){
continue;
}
data_size /=3;
return data_size;
}
if(pkt.data){
av_free_packet(&pkt);
}
if(quit){
return -1;
}
if(packet_queue_get(&audioq,&pkt,1)<0){
return -1;
}
audio_pkt_data = pkt.data;
audio_pkt_size = pkt.size;
}
}
void audio_callback(void *userdata,Uint8 *stream,int len){
AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
int len1,audio_size;
static uint8_t audio_buf[( AVCODEC_MAX_AUDIO_FRAME_SIZE * 3 ) / 2];
static unsigned int audio_buf_size = 0;
static unsigned int audio_buf_index = 0;
while(len>0){
if(audio_buf_index >= audio_buf_size){
audio_size = audio_decode_frame(aCodecCtx,audio_buf,sizeof(audio_buf));
if(audio_size<0){
audio_buf_size = 1024;
memset(audio_buf,0,audio_buf_size);
}
else{
audio_buf_size = audio_size;
}
audio_buf_index = 0;
}
len1 = audio_buf_size - audio_buf_index;
if(len1>len)
len1 = len;
memcpy(stream,(uint8_t *)audio_buf + audio_buf_index,len1);
len -=len1;
stream +=len1;
audio_buf_index +=len1;
}
}
int main()
{
AVFormatContext *pFormatCtx;
int i,audioStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVPacket packet;
float aspect_ratio;
AVCodecContext *aCodecCtx;
AVCodec *aCodec;
SDL_Event event;
SDL_AudioSpec wanted_spec,spec;
char songs[] = "D:\\2.mp3";
//Register
av_register_all();
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
puts("Could not initialize SDL!");
return 1;
}
if(av_open_input_file(&pFormatCtx,songs,NULL,0,NULL)!=0)
文章评论