diff options
| author | 2023-11-03 11:07:58 +0000 | |
|---|---|---|
| committer | 2023-11-03 11:07:58 +0000 | |
| commit | 693d020ba2a8da89adf83c19af232ca653c182fc (patch) | |
| tree | ad8fd0035ad12e7ea785b8201e99c79b99718c2c | |
| parent | 5ea095ca58eac7f5ae1011793f054ac2bebd81f8 (diff) | |
| parent | ea7cb6a022b35410d22626e48266488c4bc3c2de (diff) | |
Merge "Take offset into account while writing bytes into audio track." into main
| -rw-r--r-- | core/java/android/speech/tts/BlockingAudioTrack.java | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/core/java/android/speech/tts/BlockingAudioTrack.java b/core/java/android/speech/tts/BlockingAudioTrack.java index be5851c3e64f..d84cc2c60c3b 100644 --- a/core/java/android/speech/tts/BlockingAudioTrack.java +++ b/core/java/android/speech/tts/BlockingAudioTrack.java @@ -194,17 +194,22 @@ class BlockingAudioTrack { audioTrack.play(); } - int count = 0; - while (count < bytes.length) { - // Note that we don't take bufferCopy.mOffset into account because - // it is guaranteed to be 0. - int written = audioTrack.write(bytes, count, bytes.length); + int offset = 0; + while (offset < bytes.length) { + // Although it requests to write the entire bytes at once, it might fail when the track + // got stopped or the thread is interrupted. In that case, it needs to carry on from + // last offset. + int sizeToWrite = bytes.length - offset; + int written = audioTrack.write(bytes, offset, sizeToWrite); if (written <= 0) { + if (written < 0) { + Log.e(TAG, "An error occurred while writing to audio track: " + written); + } break; } - count += written; + offset += written; } - return count; + return offset; } private AudioTrack createStreamingAudioTrack() { |