Lesson: 25 – Audio playback (file from SD card) 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 purpose of this concept is to access an mp3 file stored on the SD card of our computer (keep in mind that this only works with a computer that has the capacity of SD card) We must use an emulator that has configured SD card, Meanwhile, you can use your mobile device.

Issue:

Provide a button labeled “cat”, then when you press play the respective audio file.┬áThe sound file will store it on the SD card.

After creating the project (Project027) we must run the application to access the SD card created by the Android emulator.

Once the application is running (it does not matter that we have not yet implemented its functionality) we proceed to open the “Android Device Monitor” window.┬áSelect from the options menu of Tools-> Android-> Android Device Monitor.

In the folder storage / sdcard we must upload the mp3 file (we must select this folder with the mouse)
We do this by means of a button that appears in the right part of this window “Push a file onto device”, after this we have the mounted file on the SD card:

Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin -

After uploading it should appear in the list of files that has the SD card:

Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin -

Now let’s implement the interface of our application (a single button):

Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin -

The source code in Kotlin is:

package com.coding180.project027


import android.media.MediaPlayer
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.Button
// coding180.com
class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
        val button1 = findViewById (R.id.button) as Button
        button1.setOnClickListener {
            val data = Uri.parse (Environment.getExternalStorageDirectory (). getPath () + "/gato.mp3")
            val mp = MediaPlayer.create (this, data)
            mp.start ()
        }
    }
}

We create an object of class Uri by calling the parse method where we indicate the path and name of the file to recover:

            val data = Uri.parse (Environment.getExternalStorageDirectory (). getPath () + "/gato.mp3")

We create the object of the MediaPlayer class by passing the object reference of the Uri class:

            val mp = MediaPlayer.create (this, data)

We start the mp3 playback:

            mp.start ()

Another important step before running the application is to add the permission to read the SD card, so we modified the file AndroidManifest.xml by adding this permission:

Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin - Lesson: 25 - Audio playback (file from SD card) in Kotlin -

…..

Remember that to run this project you must upload an mp3 file called “gato.mp3” to the SD card.

About The Author

Related posts

Leave a Reply