Saturday, 10 April 2021

Can MediaRecorder save voice as mp3 or m4a audio format file automatically based file name extension in Android Studio?

The following Code A is from the sample project.

It use the Code B to record voice and save as file.

Code B

setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)

You know the getExtensionText() will return two audio format, mp3 or m4a by user settings , there are different audio format.

I don't know why the author can use only Code B to generate two different audio format file.

Can MediaRecorder save voice as mp3 or m4a audio format file automatically based file name extension in Android Studio?

Code A

    // mp4 output format with aac encoding should produce good enough m4a files according to https://stackoverflow.com/a/33054794/1967672
    private fun startRecording() {
        val baseFolder = if (isQPlus()) {
            cacheDir
        } else {
            val defaultFolder = File(config.saveRecordingsFolder)
            if (!defaultFolder.exists()) {
                defaultFolder.mkdir()
            }

            defaultFolder.absolutePath
        }

        currFilePath = "$baseFolder/${getCurrentFormattedDateTime()}.${config.getExtensionText()}"
        recorder = MediaRecorder().apply {
            setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
            setAudioEncodingBitRate(128000)
            setAudioSamplingRate(44100)

         ...

      }



  class Config(context: Context) : BaseConfig(context) {
    ...

   
    fun getExtensionText() = context.getString(when (extension) {
        EXTENSION_M4A -> R.string.m4a
        else -> R.string.mp3
    })
 }


from Can MediaRecorder save voice as mp3 or m4a audio format file automatically based file name extension in Android Studio?

No comments:

Post a Comment