26 – Audio playback (file from internet) 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

Now let’s see the steps to play a file stored on an internet server.

Issue:

Provide a button labeled “Play from internet”, then when you press play the respective audio file. The sound file is stored in .http://urban180.com/wp-content/uploads/2017/09/olaide-Bambi_urban180.com_-1.mp3.

 

We created a project (Project028) and the first step is to modify the AndroidManifest.xml file where we authorize the application to access resources located on the internet (we must open this file and proceed to add the permission to access the internet by adding the mark uses-permission respective):

26 - Audio playback (file from internet) in Kotlin - 26 - Audio playback (file from internet) in Kotlin - 26 - Audio playback (file from internet) in Kotlin -

We create the interface of the application with a button:

26 - Audio playback (file from internet) in Kotlin - 26 - Audio playback (file from internet) in Kotlin - 26 - Audio playback (file from internet) in Kotlin -

The source code is:

package com.coding180.project028


import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import java.io.IOException

class MainActivity: AppCompatActivity () {
// coding180.com
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)

        val button1 = findViewById (R.id.button) as Button
        button1.setOnClickListener {
            val mp = MediaPlayer ()
            try {
                mp.setDataSource ("http://urban180.com/wp-content/uploads/2017/09/olaide-Bambi_urban180.com_-1.mp3")
                mp.prepare ()
                mp.start ()
            } catch (e: IOException) {
                Toast.makeText (this, "The file does not exist", Toast.LENGTH_LONG) .show ()
            }
        }
    }
}

To recover an mp3 file from the internet we proceed as follows, we first create an object of the MediaPlayer class:

            val mp = MediaPlayer ()

Then we call the setDataSource method indicating the internet address where the mp3 file is stored:

                mp.setDataSource ("http://urban180.com/wp-content/uploads/2017/09/olaide-Bambi_urban180.com_-1.mp3")

We call the prepare method and then call start:

                mp.prepare ()
                mp.start ()

All this we do in a try/catch block to catch exceptions of type IOException.

This first approach to run an mp3 located on the internet blocks the application until the file is fully loaded, ie the mp.prepare () method is executed until the recovery is complete.

….

Issue 2:

Make another application similar to the previous one but do not freeze the interface of the application while loading the mp3. Show a message that the file is loading.

We developed a new project (Project029), assigned permission to access the internet in the file AndroidManifest.xml and created an interface similar to the previous problem.

The source code is:

package com.coding180.project029


import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import java.io.IOException

class MainActivity: AppCompatActivity () , MediaPlayer.OnPreparedListener{

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
// coding180.com
        val buton = findViewById (R.id.button) as Button
        buton.setOnClickListener {
            val mp = MediaPlayer ()
            mp.setOnPreparedListener(this)
            try {
                mp.setDataSource ("http://urban180.com/wp-content/uploads/2017/09/olaide-Bambi_urban180.com_-1.mp3")
                mp.prepareAsync ()
            } catch (e: IOException) {
            }
            Toast.makeText (this, "Wait a moment while loading the mp3", Toast.LENGTH_SHORT) .show ()
        }
    }

    override fun onPrepared (p0: MediaPlayer?) {
        p0?.start()
    }

}

In order to capture the event that the file was finished recovering we must implement the MediaPlayer.OnPreparedListener interface:

class MainActivity: AppCompatActivity (), MediaPlayer.OnPreparedListener {

With this we say that our class will implement the onPrepared method where we start the mp3 execution:

    override fun onPrepared (p0: MediaPlayer?) {
        p0? .start ();
    }

In the click event of the button, we create the object of the MediaPlayer class, we pass to the method setOnPreparedListener the address of the object that will capture the event that the resource is complete. Then we call the setDataSource and prepareAsync methods to initialize the mp3 load. Finally, we show a message to inform the user that the file is being downloaded:

        button.setOnClickListener {
            val mp = MediaPlayer ()
            mp.setOnPreparedListener (this)
            try {
                mp.setDataSource ("http://urban180.com/wp-content/uploads/2017/09/olaide-Bambi_urban180.com_-1.mp3")
                mp.prepareAsync ()
            } catch (e: IOException) {
            }
            Toast.makeText (this, "Wait a moment while loading the mp3", Toast.LENGTH_SHORT) .show ()
        }

….

 

About The Author

Related posts

Leave a Reply