AudioTrack lag: obtainBuffer timed out

后端 未结 2 1746
眼角桃花
眼角桃花 2021-02-05 22:02

I\'m playing WAVs on my Android phone by loading the file and feeding the bytes into AudioTrack.write() via the FileInputStream > BufferedInputStream > DataInputStream method.

2条回答
  •  没有蜡笔的小新
    2021-02-05 22:27

    A bit late to the party answering this, but in case it helps anyone in the future - I ran into this exact problem with code pretty similar to the code in the question, where the AudioTrack is created and set to play, but not written to immediately.

    I found that creating the AudioTrack immediately before you start writing to it made the delay go away. For some reason AudioTrack doesn't seem to like sitting around with an empty buffer.

    In terms of the code above, you'd want to do something like

    mAudioTrack=null;
    while (mRun) 
    { 
    
        // This flag is turned on when the user presses "play" 
        while (mPlaying) 
        { 
    
            try 
            { 
                if (mAudioTrack==null) {   
                    mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, ConfigManager.SAMPLE_RATE,AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT,ConfigManager.AUDIO_BUFFER_LENGTH,AudioTrack.MODE_STREAM);   
                    mAudioTrack.play();   
                }
                // Rest of the playback code here
            }
        }
        mAudioTrack=null;
    }
    

提交回复
热议问题