Search code examples
androidkotlinandroid-mediarecorder

Audio recording in Android (with kotlin)


I am working on a trial app to do audio recording in Android (with kotlin).

Here is the problem I currently hit on the way.

In the following code:

recordBtn.setOnClickListener {
    if (applicationContext.checkSelfPermission(
            Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        val permissions = arrayOf(android.Manifest.permission.RECORD_AUDIO)
        ActivityCompat.requestPermissions(this, permissions,0)
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            startRecording()
        }
    }
}

The startRecording() function does not get excuted because: Build.VERSION.SDK_INT = 28 and Build.VERSION_CODES.S = 31 And if I remove the test to enforce the excution of startRecording(), then as one could expect, the app crashes.

My question is: what can I do to have a higher Build.VERSION.SDK_INT at least on my own device?

The research I have done to find the answer on the net do not lead me to a clear answer.

To allow further comment here is the code containing the startRecording() function:

@SuppressLint("RestrictedApi")
@RequiresApi(Build.VERSION_CODES.S)
private fun startRecording() {
    //isRecording = true
    stopBtn.isEnabled = true
    playBtn.isEnabled = false
    recordBtn.isEnabled = false

    val theAudioFile = File(getFilesDir(), "Audio_Record_File")

    mediaRecorder = MediaRecorder(applicationContext).apply {
        setAudioSource(MediaRecorder.AudioSource.MIC)
        setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
        setOutputFile(theAudioFile)
        setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)

        try {
            prepare()
        } catch (e: IOException) {
            Log.e(LOG_TAG, "prepare() failed")
        }

        start()
    }
}

The code below to set mediaRecorder, does not work either. Android Studio puts an horizontal line accross MediaRecorder() even if I write:

val mediaRecorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    MediaRecorder(applicationContext)
} else {
    MediaRecorder()
}.apply {
    ......
}

And I see this message in the list of problems:

Call requires API level 31 (current min is 26): `startRecording`

Solution

  • First of all you should take a look here: https://developer.android.com/reference/android/media/MediaRecorder

    As you can see constructor public MediaRecorder (Context context) added only in API 31.

    You can try to use deprecated constructor for API lower than 31, like this:

    val mediaRecorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            MediaRecorder(applicationContext)
        } else {
            MediaRecorder()
        }.apply {
            setAudioSource(MediaRecorder.AudioSource.MIC)
            setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
            setOutputFile(theAudioFile.path)
            setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
    
            try {
                prepare()
            } catch (e: IOException) {
    
            }
    
            start()
        }
    

    And remove your @RequiresApi(Build.VERSION_CODES.S) annotation and if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) check before you call startRecording.