Skip to content

Commit 644a50b

Browse files
committed
Update android.kt
1 parent 1ff1618 commit 644a50b

File tree

1 file changed

+6
-3
lines changed
  • firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/util

1 file changed

+6
-3
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/util/android.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package com.google.firebase.vertexai.common.util
1818

1919
import android.media.AudioRecord
20+
import kotlinx.coroutines.delay
2021
import kotlinx.coroutines.flow.flow
21-
import kotlinx.coroutines.yield
2222

2323
/**
2424
* The minimum buffer size for this instance.
@@ -31,14 +31,17 @@ internal val AudioRecord.minBufferSize: Int
3131
/**
3232
* Reads from this [AudioRecord] and returns the data in a flow.
3333
*
34-
* Will emit a zeroed out buffer when this instance is not recording.
34+
* Will emit a zeroed out buffer every 100ms when this instance is not recording.
3535
*/
3636
internal fun AudioRecord.readAsFlow() = flow {
3737
val buffer = ByteArray(minBufferSize)
38+
val emptyBuffer = ByteArray(minBufferSize)
3839

3940
while (true) {
4041
if (recordingState != AudioRecord.RECORDSTATE_RECORDING) {
41-
yield()
42+
emit(emptyBuffer)
43+
// The model will close the connection if you spam it too fast
44+
delay(100)
4245
continue
4346
}
4447

0 commit comments

Comments
 (0)