Skip to content

Commit 7b21f07

Browse files
Implemented user suggestion (#656)
1 parent fde56c0 commit 7b21f07

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

exampleCustomAudioDevice/src/main/java/com/twilio/examplecustomaudiodevice/FileAndMicAudioDevice.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,31 @@ public class FileAndMicAudioDevice implements AudioDevice {
125125
this.releaseAudioResources();
126126
return;
127127
}
128+
try {
129+
while (keepAliveRendererRunnable) {
130+
// Get 10ms of PCM data from the SDK. Audio data is written into the ByteBuffer provided.
131+
AudioDevice.audioDeviceReadRenderData(renderingAudioDeviceContext, readByteBuffer);
128132

129-
while (keepAliveRendererRunnable) {
130-
// Get 10ms of PCM data from the SDK. Audio data is written into the ByteBuffer provided.
131-
AudioDevice.audioDeviceReadRenderData(renderingAudioDeviceContext, readByteBuffer);
132-
133-
int bytesWritten = 0;
134-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
135-
bytesWritten = writeOnLollipop(audioTrack, readByteBuffer, readByteBuffer.capacity());
136-
} else {
137-
bytesWritten = writePreLollipop(audioTrack, readByteBuffer, readByteBuffer.capacity());
138-
}
139-
if (bytesWritten != readByteBuffer.capacity()) {
140-
Log.e(TAG, "AudioTrack.write failed: " + bytesWritten);
141-
if (bytesWritten == AudioTrack.ERROR_INVALID_OPERATION) {
142-
keepAliveRendererRunnable = false;
143-
break;
133+
int bytesWritten = 0;
134+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
135+
bytesWritten = writeOnLollipop(audioTrack, readByteBuffer, readByteBuffer.capacity());
136+
} else {
137+
bytesWritten = writePreLollipop(audioTrack, readByteBuffer, readByteBuffer.capacity());
138+
}
139+
if (bytesWritten != readByteBuffer.capacity()) {
140+
Log.e(TAG, "AudioTrack.write failed: " + bytesWritten);
141+
if (bytesWritten == AudioTrack.ERROR_INVALID_OPERATION) {
142+
keepAliveRendererRunnable = false;
143+
break;
144+
}
144145
}
146+
// The byte buffer must be rewinded since byteBuffer.position() is increased at each
147+
// call to AudioTrack.write(). If we don't do this, will fail the next AudioTrack.write().
148+
readByteBuffer.rewind();
145149
}
146-
// The byte buffer must be rewinded since byteBuffer.position() is increased at each
147-
// call to AudioTrack.write(). If we don't do this, will fail the next AudioTrack.write().
148-
readByteBuffer.rewind();
150+
} catch (IllegalStateException error) {
151+
error.printStackTrace();
152+
releaseAudioResources();
149153
}
150154
};
151155

0 commit comments

Comments
 (0)