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.
The usable formats listed in the MediaRecorder.OutputFormat are:
AAC_ADTS
AMR_NB
AMR_WB
MPEG_2_TS
MPEG_4
OGG
RAW_AMR (Deprecated)
THREE_GPP
WEBM
If you are adamant on using the WAV format, you might have to encode the WAV file using the audio data as mentioned here.