以下一般问题:我解码音频如下:
ReSampleContext* rsc = av_audio_resample_init(
1, aCodecCtx->channels,
aCodecCtx->sample_rate, aCodecCtx->sample_rate,
av_get_sample_fmt("u8"), aCodecCtx->sample_fmt,
16, 10, 0, 1);
while (av_read_frame(pFormatCtx, &packet)>= 0) {
if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 2;
int size=packet.size;
int decoded = 0;
while(size > 0) {
int len = avcodec_decode_audio3(aCodecCtx, pAudioBuffer,
&data_size, &packet);
//Сonvert audio to sample 8bit
out_size = audio_resample(rsc, outBuffer, pAudioBuffer, len);
jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);
memcpy(bytes, outBuffer, out_size);
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
(*env)->CallStaticVoidMethod(env, cls, mid, array, out_size, number);
size -= len;
number++;
}
}
}
接下来发布AudioTrack。在那之后,我听到那首必要的歌曲,但噪音和速度要大2倍。可能是什么问题?
更新:这是Java代码:
public static AudioTrack track;
public static byte[] bytes;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int bufSize = 2048;
track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_8BIT, bufSize, AudioTrack.MODE_STREAM);
bytes = new byte[bufSize];
Thread mAudioThread = new Thread(new Runnable() {
public void run() {
int res = main(2, "/sdcard/muzika_iz_reklami_bmw_5_series_-_bmw_5_series.mp3", bytes);
System.out.println(res);
}
});
mAudioThread.setPriority(Thread.MAX_PRIORITY);
mAudioThread.start();
}
private static void play(byte[] play, int length, int p) {
if (p==0){
track.play();
}
track.write(play, 0, length);
}
这可能是采样率不匹配的问题。可能是您创建的audioTrack采样率与实际采样率相比较低。
正如马修所提到的,它可能也是AudioTrack频道配置的问题。
也许你应该尝试像我在这个question回答的那样。
因为你从Java与JNI交互的方式,我不确定,这是否有效。我对JNI知之甚少。但我的代码与我合作,这是我目前使用我的应用程序。
也许你的AudioTrack
期待立体声数据,但你发送它是单声道的。您可以尝试将AudioTrack
通道配置设置为CHANNEL_OUT_MONO。