开发者

Audio playing back at the wrong speed using FFmpeg on Android

开发者 https://www.devze.com 2023-02-20 20:41 出处:网络
General problem in the following: I decode the audio as follows: ReSampleContext* rsc = av_audio_resample_init(

General problem in the following: I decode the audio as follows:

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++;
         }
     }
 }

Next release it AudioTrack. After that, I hear that song that was necessary, but with noise and speed of 2 t开发者_Python百科imes larger. In what may be the problem?

UPDATE: This is Java code:

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); 
}


Perhaps your AudioTrack is expecting stereo data but you are sending it mono. You could try setting your AudioTrack channel configuration to CHANNEL_OUT_MONO.


This could be a problem of sampling rate mismatch. May be you are creating audioTrack with a lesser sampler rate compared to actual rate.

It could be also a problem with the AudioTrack channel configuration, as Matthew mentioned.

May be you should try something like I have answered in this question.

Because the way you are interacting with JNI from Java, I am not sure, whether that will work. I have little knowledge about JNI. But my code works with me and which is what I am currently using with my app.

0

精彩评论

暂无评论...
验证码 换一张
取 消