Lesson 28: Record audio using Android recorder (via Intent) in Kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

The easiest way to capture audio on Android is through the recorder provided by the Android operating system. We invoke the recording application and then recover the recorded audio.

It has the advantage that the interface is familiar to the user, since many applications use this feature.

Issue:

Arrange two objects of the Button class with the labels “record” and “play”. When you press the first button to proceed to activate the recorder provided by Android. When the second button is pressed play the recorded audio.

Create a project (Project031) and implement the interface:

 

Lesson 28: Record audio using Android recorder (via Intent) in Kotlin - Lesson 28: Record audio using Android recorder (via Intent) in Kotlin - Lesson 28: Record audio using Android recorder (via Intent) in Kotlin -

The source code in Kotlin is:

package com.coding180.project031



import android.app.Activity
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.provider.MediaStore
import android.content.Intent
import android.media.MediaPlayer

class MainActivity: AppCompatActivity () {
    private val request = 1
    private lateinit var url1: Uri
// coding180.com
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)

        val button1 = findViewById (R.id.button1) as Button
        button1.setOnClickListener {
            val intent = Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION)
            startActivityForResult (intent, request)
        }

        val button2 = findViewById (R.id.button2) as Button
        button2.setOnClickListener {
            val mediaPlayer = MediaPlayer.create (this, url1)
            mediaPlayer.start ()
        }
    }

    override fun onActivityResult (requestCode: Int, resultCode: Int, data: Intent) {
        if (resultCode == Activity.RESULT_OK && requestCode == request) {
            url1 = data.data
        }
    }
}

When you press the button to record the audio using an Intent we activate the Android recording application itself.
Next, we call the method startActivityForResult to be able to recover the recording after it is finished through the onActivityResult method:

val button1 = findViewById (R.id.button1) as Button
        button1.setOnClickListener {
            val intent = Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION)
            startActivityForResult (intent, request)
        }

We must pass to the method startActivityForResult in addition to the reference of Intent a variable with a value 0 or positive (then this value will return to the onActivityResult method)

When we finish recording, the onActivityResult method is executed, where we store in the url1 variable the reference to the created audio file:

override fun onActivityResult (requestCode: Int, resultCode: Int, data: Intent) {
        if (resultCode == Activity.RESULT_OK && requestCode == request) {
            url1 = data.data
        }
    }

Finally, to execute the contents of the recording we use the class already seen MediaPlayer:

val button2 = findViewById (R.id.button2) as Button
        button2.setOnClickListener {
            val mediaPlayer = MediaPlayer.create (this, url1)
            mediaPlayer.start ()
        }

….

About The Author

Related posts

Leave a Reply