- (NSInteger)decode {
if (inBuffer_) return decodedDataSize_;
AVFrame *frame = avcodec_alloc_frame();
if (!frame) {
NSLog(@"Could not allocate audio frame\n");
return -1;
}
frame->nb_samples = audioCodecContext_->frame_size;
frame->format = audioCodecContext_->sample_fmt;
frame->channel_layout = audioCodecContext_->channel_layout;
decodedDataSize_ = 0;
AVPacket *packet = [self readPacket];
while (packet && packet->size > 0) {
if (audioBufferSize_ < FFMAX(packet->size * sizeof(*audioBuffer_), AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
audioBufferSize_ = FFMAX(packet->size * sizeof(*audioBuffer_), AVCODEC_MAX_AUDIO_FRAME_SIZE);
av_free(audioBuffer_);
audioBuffer_ = av_malloc(audioBufferSize_);
}
decodedDataSize_ = audioBufferSize_;
//NSInteger len = avcodec_decode_audio3(audioCodecContext_,audioBuffer_, &decodedDataSize_, packet);
NSInteger len = avcodec_decode_audio4(audioCodecContext_, frame, &decodedDataSize_, packet);
//len = 208;
if (len < 0) {
NSLog(@"Could not decode audio packet.");
return 0;
}
if (decodedDataSize_ <= 0) {
NSLog(@"Decoding was completed.");
packet = NULL;
return 0;
}
//decodedDataSize_ = av_samples_get_buffer_size(NULL, audioCodecContext_->channels, frame->nb_samples, audioCodecContext_->sample_fmt, 1);
memcpy(audioBuffer_,frame->data[0],frame->nb_samples);
packet->data += len;
packet->size -= len;
packet->pts = AV_NOPTS_VALUE;
decodedDataSize_ = audioCodecContext_->frame_size*audioCodecContext_->channels;
inBuffer_ = YES;
break;
}
return decodedDataSize_;
}经过测试,模拟器上所有格式音频文件都可正常播放,只是真机上出现杂音,求高手指点!!!
if (inBuffer_) return decodedDataSize_;
AVFrame *frame = avcodec_alloc_frame();
if (!frame) {
NSLog(@"Could not allocate audio frame\n");
return -1;
}
frame->nb_samples = audioCodecContext_->frame_size;
frame->format = audioCodecContext_->sample_fmt;
frame->channel_layout = audioCodecContext_->channel_layout;
decodedDataSize_ = 0;
AVPacket *packet = [self readPacket];
while (packet && packet->size > 0) {
if (audioBufferSize_ < FFMAX(packet->size * sizeof(*audioBuffer_), AVCODEC_MAX_AUDIO_FRAME_SIZE)) {
audioBufferSize_ = FFMAX(packet->size * sizeof(*audioBuffer_), AVCODEC_MAX_AUDIO_FRAME_SIZE);
av_free(audioBuffer_);
audioBuffer_ = av_malloc(audioBufferSize_);
}
decodedDataSize_ = audioBufferSize_;
//NSInteger len = avcodec_decode_audio3(audioCodecContext_,audioBuffer_, &decodedDataSize_, packet);
NSInteger len = avcodec_decode_audio4(audioCodecContext_, frame, &decodedDataSize_, packet);
//len = 208;
if (len < 0) {
NSLog(@"Could not decode audio packet.");
return 0;
}
if (decodedDataSize_ <= 0) {
NSLog(@"Decoding was completed.");
packet = NULL;
return 0;
}
//decodedDataSize_ = av_samples_get_buffer_size(NULL, audioCodecContext_->channels, frame->nb_samples, audioCodecContext_->sample_fmt, 1);
memcpy(audioBuffer_,frame->data[0],frame->nb_samples);
packet->data += len;
packet->size -= len;
packet->pts = AV_NOPTS_VALUE;
decodedDataSize_ = audioCodecContext_->frame_size*audioCodecContext_->channels;
inBuffer_ = YES;
break;
}
return decodedDataSize_;
}经过测试,模拟器上所有格式音频文件都可正常播放,只是真机上出现杂音,求高手指点!!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货