We are using AudioRecord to record the audio and display the recording as a waveform using a library. The code to record the audio and display the waveform is posted below. But for some reason, I am getting this error at 1. Can someone help me on how can I resolve this issue? This happened just once in 3 months after the app has gone live but would like to understand the issue and put in a fix for the same.
This has happened in Android 5.1.1 device
inner class RecordingThread : Thread() {
var mShouldContinue = true;
override fun run() {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO)
val recorder = AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, RECORDING_SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, byteBufferSize)
try {
recorder.startRecording()
while (shouldContinue()) {
recorder.read(audioByteBuffer, 0, byteBufferSize)
Thread({
val buffer = ByteBuffer.wrap(audioByteBuffer)
buffer.order(ByteOrder.LITTLE_ENDIAN)
buffer.put(audioByteBuffer)
try {
output?.write(buffer.array())
} catch (exception: Exception) {
error(exception)
}
}).start() ----------------------------- Error at this place
Thread({
val shorts = kotlin.ShortArray(byteBufferSize/2)
ByteBuffer.wrap(audioByteBuffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
recordingWaveform?.updateAudioData(shorts)
}).start()
}
} catch (e: IllegalStateException) {
error(e)
} finally {
recorder.stop()
recorder.release()
}
}
/**
* Gets a value indicating whether the thread should continue running.
* @return true if the thread should continue running or false if it should stop
*/
@Synchronized private fun shouldContinue(): Boolean {
return mShouldContinue
}
/** Notifies the thread that it should stop running at the next opportunity. */
@Synchronized fun stopRunning() {
mShouldContinue = false
}
}
while
loop iteration. Are you sure there threads actually terminate some time later? If you are just keeping creating threads, that's definitely a problem. – Mikhail Naganov Jun 16 '16 at 16:05while
loop where you create and start two threads. The loop doesn't wait until the threads complete, it just goes into next iteration and creates two new threads again and again. Although these threads are terminating at some point, there may be a lot of them running at some given moment. And eventually you might just exhaust OS capabilities, that's why an attempt to create one more thread fails. I would first try to measure how many threads you usually have running simultaneously. – Mikhail Naganov Jun 17 '16 at 8:01