summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/java/android/speech/tts/BlockingAudioTrack.java19
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() {