File tree 2 files changed +31
-5
lines changed
firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type 2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,21 @@ internal class AudioHelper {
69
69
}
70
70
}
71
71
72
+ fun stopRecording () {
73
+ if (
74
+ ::audioRecord.isInitialized && audioRecord.recordingState == AudioRecord .RECORDSTATE_RECORDING
75
+ ) {
76
+ audioRecord.stop()
77
+ }
78
+ }
79
+
80
+ fun start () {
81
+ if (
82
+ ::audioRecord.isInitialized && audioRecord.recordingState != AudioRecord .RECORDSTATE_RECORDING
83
+ ) {
84
+ audioRecord.startRecording()
85
+ }
86
+ }
72
87
@RequiresPermission(Manifest .permission.RECORD_AUDIO )
73
88
fun startRecording (): Flow <ByteArray > {
74
89
@@ -110,10 +125,16 @@ internal class AudioHelper {
110
125
return flow {
111
126
val buffer = ByteArray (bufferSize)
112
127
while (! stopRecording) {
113
- val bytesRead = audioRecord.read(buffer, 0 , buffer.size)
114
- if (bytesRead > 0 ) {
115
- emit(buffer.copyOf(bytesRead))
128
+ if ( audioRecord.recordingState != AudioRecord . RECORDSTATE_RECORDING ) {
129
+ buffer.fill( 0x00 )
130
+ continue
116
131
}
132
+ try {
133
+ val bytesRead = audioRecord.read(buffer, 0 , buffer.size)
134
+ if (bytesRead > 0 ) {
135
+ emit(buffer.copyOf(bytesRead))
136
+ }
137
+ } catch (_: Exception ) {}
117
138
}
118
139
}
119
140
}
Original file line number Diff line number Diff line change @@ -183,8 +183,13 @@ internal constructor(
183
183
private fun playServerResponseAudio () {
184
184
CoroutineScope (backgroundDispatcher).launch {
185
185
while (isRecording) {
186
- val x = playBackQueue.poll() ? : continue
187
- audioHelper?.playAudio(x)
186
+ val data = playBackQueue.poll()
187
+ if (data == null ) {
188
+ audioHelper?.start()
189
+ continue
190
+ }
191
+ audioHelper?.stopRecording()
192
+ audioHelper?.playAudio(data)
188
193
}
189
194
}
190
195
}
You can’t perform that action at this time.
0 commit comments