I'm currently working on a school project, where I'm creating a sort of clone of Shazam. I'm at the point where I can successfully record audio, and decode it into a base64string, just like the api asks, but it doesn't recognize any songs, since it's probably a wrong decoding process or wrong recording process. At the moment, I'm manually inserting my data into the api, so connection issues are out of the question.
This is what the api asks: Encoded base64 string of byte[] that generated from raw data less than 500KB (3-5 seconds sample are good enough for detection). The raw sound data must be 44100Hz, 1 channel (Mono), signed 16 bit PCM little endian. Other types of media are NOT supported, such as : mp3, wav, e...
I have a AudioRecorder class that handles the recording of the audio:
override fun start(outputFile: File) {
createRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
setAudioChannels(1)
setAudioSamplingRate(44100)
setAudioEncodingBitRate(16 * 44100)
setOutputFile(outputFile.absolutePath)
prepare()
start()
recorder = this
}
}
override fun stop() {
recorder?.stop()
recorder?.reset()
recorder = null
}
fun readAudioFile(file: File): ByteArray {
return file.readBytes()
}
Then i have a function that handles my recording, this gets executed at the right time as well
private suspend fun launchAudioRecording(audioRecorder: AudioRecorder, context: Context): String {
Log.d("be.howest.rythmradar.ui.SearchingSongScreen", "be.howest.rythmradar.ui.launchAudioRecording - Start")
// Start recording
File(context.cacheDir, "audio.3gp").also {
audioRecorder.start(it)
audioFile = it
}
// Delay for 5 seconds to record audio
delay(5000)
// Stop recording
audioRecorder.stop()
// Do something with the recorded audio, such as converting it to Base64 string
val audioData = audioRecorder.readAudioFile(audioFile!!)
val base64String = convertToBase64String(audioData)
return base64String
}
And finally my base64string converter:
private fun convertToBase64String(audioData: ByteArray): String {
return Base64.encodeToString(audioData, Base64.DEFAULT)
}
I recently worked on a similar project. RapidApi doesn't work for me either but AudDApi! does. I managed to get hold of my base64String into a hidden form input element by setting its value to base64String and passed this to AudDApi which works perfectly. However, audio recording must not exceed 5 seconds else results in payload error.
You may try the code below to tweak your audio recording and base65String.