I am a beginner for kotlin and android application.
I would like to record voice continously and pass the voice data to server via socket.
I tried the code below but the server ended up receiving data in AMR format only.
val byteArrayOutputStream = ByteArrayOutputStream()
val descriptors = ParcelFileDescriptor.createPipe()
val parcelRead = ParcelFileDescriptor(descriptors[0])
val parcelWrite = ParcelFileDescriptor(descriptors[1])
val inputStream: InputStream = ParcelFileDescriptor.AutoCloseInputStream(parcelRead)
val recorder = MediaRecorder()
recorder.setAudioSource(MediaRecorder.AudioSource.MIC)
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB)
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
recorder.setOutputFile(parcelWrite.fileDescriptor)
recorder.prepare()
recorder.start()
var read: Int
val data = ByteArray(16384)
while ((inputStream.read(data, 0, data.size).also { read = it } != -1)) {
byteArrayOutputStream.write(data, 0, read)
}
byteArrayOutputStream.flush()
How can I use byteArrayOutputStream to send audio data in WAV format? Is it possible to modify the sampling rate to 48000 using the above code?
Thank you.