27 – Playing audio using your own Android player (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

Another way to run an mp3 file is through the built-in Android player. This application plays all formats supported by Android and has an interface that will be familiar to the user of our application.

Issue:

Provide a button with the tag: “run mp3 with the own android player”, then when you press play the respective audio file with the Android player via Intent. The sound file will store it on the SD card, we will use the file “gato.mp3” which we upload to the SD card in previous concepts.

We will create Project030.

We create the interface with the Button:

 

27 - Playing audio using your own Android player (via Intent) in kotlin - 27 - Playing audio using your own Android player (via Intent) in kotlin - 27 - Playing audio using your own Android player (via Intent) in kotlin -

The source code is:

package com.coding180.project030


import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
        // coding180.com
        val button = findViewById (R.id.button) as Button
        button.setOnClickListener {
            val intent = Intent (Intent.ACTION_VIEW)
            val data = Uri.parse ("file:///sdcard"+"/gato.mp3")
            intent.setDataAndType (data, "audio/mp3")
            startActivity (intent)
        }
    }
}

We create an Intent class object and a Uri class object by referencing the mp3 file stored on the SD card. We indicate using the setDataAndType method the Uri and the type of file to be played. We activate the application using startActivity.

When we press the button we see how the android player is activated and is the one that actually plays the audio file:

27 - Playing audio using your own Android player (via Intent) in kotlin - 27 - Playing audio using your own Android player (via Intent) in kotlin - 27 - Playing audio using your own Android player (via Intent) in kotlin -

About The Author

Related posts

Leave a Reply