Open In App
Related Articles

How to Play Audio from URL in Android?

Like Article
Save Article
Report issue

Many apps require the feature to add the audio feature in their application and there so many audio files that we have to play inside our application. If we will store so many audio files inside our application, then it will increase the size of the app and this may reduce the user base due to the huge app size. So the better way to tackle this problem is to store the audio files in your database and access them from their unique web URL. In this article, we will take a look at playing an audio file from URL in Android. 

What we are going to build in this application?   

We will be building a simple application in which we will display two buttons for play and pause of our audio. We will be loading that audio from the URL. Below is the video in which we will get to see what we are going to build in this article. Now let’s move towards the implementation. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Button for playing audio-->
        android:text="Play Audio file"
        android:textAllCaps="false" />
    <!--Button for pausing the audio-->
        android:text="Pause Audio"
        android:textAllCaps="false" />


Step 3: Adding permissions to the AndroidManifest.xml file

As we are playing audio from URL in android. So we will have to add Internet permissions to load URL. Add below permissions to the AndroidManifest.xml file.


<!-- Permissions of internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Step 4: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    // creating a variable for 
    // button and media player
    Button playBtn, pauseBtn;
    MediaPlayer mediaPlayer;
    protected void onCreate(Bundle savedInstanceState) {
        // initializing our buttons
        playBtn = findViewById(;
        pauseBtn = findViewById(;
        // setting on click listener for our play and pause buttons.
        playBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // calling method to play audio.
        pauseBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // checking the media player 
                // if the audio is playing or not.
                if (mediaPlayer.isPlaying()) {
                    // pausing the media player if media player 
                    // is playing we are calling below line to
                    // stop our media player.
                    // below line is to display a message 
                    // when media player is paused.
                    Toast.makeText(MainActivity.this, "Audio has been paused", Toast.LENGTH_SHORT).show();
                } else {
                    // this method is called when media 
                    // player is not playing.
                    Toast.makeText(MainActivity.this, "Audio has not played", Toast.LENGTH_SHORT).show();
    private void playAudio() {
        // initializing media player
        mediaPlayer = new MediaPlayer();
        // below line is use to set the audio 
        // stream type for our media player.
        // below line is use to set our 
        // url to our media player.
        try {
            // below line is use to prepare
            // and start our media player.
        } catch (IOException e) {
        // below line is use to display a toast message.
        Toast.makeText(this, "Audio started playing..", Toast.LENGTH_SHORT).show();


After adding the code now run your app and see the output of the app. 


Note: After clicking on the play button wait for some time as we are loading our audio file from the URL. So it will take a little bit of time to load our audio file. 

Last Updated : 20 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads