AudioTrack在Android应用中每秒都会发出咔嗒声

时间:2014-12-04 01:21:38

标签: java android audio audiotrack

我有以下代码从队列中读取包含PCM数据的对象并将PCM数据写入Android音频

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO);
    try {
        Thread.sleep(3000L);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            44100,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT),
            AudioTrack.MODE_STREAM);
    keepPlaying.set(true);
    audioTrack.play();
    while (keepPlaying.get()) {
        if (!decodedAudioQueue.isEmpty()) {
            DecodedTimedAudioChunk chunk = decodedAudioQueue.poll();
            byte[] arr = chunk.getPcmData();
            audioTrack.write(arr, 0, arr.length);
            continue;
        } else {
            Log.e(TAG, "NO audio in queue to play! ");
            continue;
        }

每个decodeaudiochunk都有一个长度为88,200的字节数组。音频是16位单声道@ 44100hz。

问题是我每秒都听到一声“砰砰”的声音。音乐仍然连贯,但弹出使我的应用程序看起来像最终用户一样。

什么可能导致这种爆裂声?它似乎每DecodedAudioChunk发生一次。

0 个答案:

没有答案