public void onRadioClick(View v) {

    if (!isPLAYING) {
        isPLAYING = true;
        MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(getString(R.string.audio_stream));
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    } else {
        isPLAYING = false;
        stopPlaying();
    }
}

private void stopPlaying() {
    mp.release();
    mp = null;
}
Answer from Hades on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › android › how-to-play-audio-from-url-in-android
How to Play Audio from URL in Android? - GeeksforGeeks
July 23, 2025 - 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.
Discussions

android - How can I play a mp3 without download from the url? - Stack Overflow
I don't want to download the mp3, I need to play from an url. ... Look at basic Android-Music Player demo MusicDroid - Audio Player Part II it describe how to use Service and AIDl for your Audio Player. More on stackoverflow.com
🌐 stackoverflow.com
[Feature Request] Play audio from URL
Tasker Feature Requests submissions website ;) More on reddit.com
🌐 r/tasker
25
6
November 11, 2022
java - Simple audio player to play music using a URL built using MediaPlayer in Android - Code Review Stack Exchange
I wanted to create a very basic application for Android that plays an audio file based on a URL input. I would like it to be reviewed based on good programming practices. This is the first time tha... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
November 19, 2017
android - Play audio from url one after other - Stack Overflow
I want to play/stream audio from url one after other automatically. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Dev2QA
dev2qa.com › home › android play local / url audio with progressbar example
Android Play Local / URL Audio With ProgressBar Example Windows Tricks
October 6, 2021 - Call MediaPlayer‘s setDataSource(Uri uri) method to set the audio file URI or path ( local file path or web URL). audioPlayer.setDataSource(getApplicationContext(), audioFileUri); Call the MediaPlayer object’s prepare() method to prepare. Call the MediaPlayer object’s start, pause, stop, release, seekTo, isPlaying, getDuration method as you need. When the activity is destroyed, do not forget to release the MediaPlayer object by it’s release method. You should declare the below permissions in the AndroidManifest.xml file to play local or URL audio.
🌐
Flutter-code
flutter-code.com › 2017 › 08 › android-play-audio-from-url-http.html
android - Play audio from url http streaming example
August 19, 2017 - Throws IllegalStateException : if it is called in an invalid state IOException */ // Prepare the media player mPlayer.prepare(); // Start playing audio from http url mPlayer.start(); // Inform user for audio streaming Toast.makeText(mContext,"Playing",Toast.LENGTH_SHORT).show(); }catch (IOException e){ // Catch the exception e.printStackTrace(); }catch (IllegalArgumentException e){ e.printStackTrace(); }catch (SecurityException e){ e.printStackTrace(); }catch (IllegalStateException e){ e.printStackTrace(); } mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mediaPlayer) { Toast.makeText(mContext,"End",Toast.LENGTH_SHORT).show(); mButtonPlay.setEnabled(true); } }); } }); } }
🌐
GitHub
github.com › HugoMatilla › AudioPlayerView
GitHub - HugoMatilla/AudioPlayerView: AudioPlayerView is an Android view that loads audio from an url and have basic playback tools.
AudioPlayerView is an Android view that loads audio from an url and have basic playback tools.
Starred by 84 users
Forked by 18 users
Languages   HTML 38.9% | Java 31.2% | CSS 29.9% | HTML 38.9% | Java 31.2% | CSS 29.9%
🌐
Reddit
reddit.com › r/tasker › [feature request] play audio from url
r/tasker on Reddit: [Feature Request] Play audio from URL
November 11, 2022 -

I'd like to make Tasker play audio directly from a url, this applies to a lot of programs/podcats and other long audios you may want to listen to while you drive/commute/walk/on the go.

I think the way Tasker implemented the "Music Play Dir" action is the right way, meaning that the action will trigger the audio and will require another action to stop it, rather than keep the task alive and get Tasker stuck.

Yes, there are a lot of apps that could help in this regard but because this is a quite simple action and the fact that Tasker shines above all other apps is circumstances. All the other phone setup before you start listening, for me, it always disappointing to set everything up with Tasker and then launch 3rd party software that does one simple action.

Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 55772542 › play-audio-from-url-one-after-other
android - Play audio from url one after other - Stack Overflow
final String audioUrl[] = {"https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/NV.wav?alt=media", "https://www.googleapis.com/storage/v1/b/ezconnectpoc/o/cqo.wav?alt=media"}; backtobackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(audioUrl[currentTrack]); mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); } }); mediaPlayer.setOnC
🌐
Tutlane
tutlane.com › tutorial › android › android-audio-media-player-with-examples
Android Audio / Media Player with Examples - Tutlane
We have multiple ways to play audio or video but the most important component of media framework is MediaPlayer class. In android, by using MediaPlayer class we can access audio or video files from application (raw) resources, standalone files in file system or from a data stream arriving over a network connection and play audio or video files with the multiple playback options such as play, pause, forward, backward, etc.
🌐
YouTube
youtube.com › watch
Play Audio From URL in Android Studio Example ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
GeeksforGeeks
geeksforgeeks.org › kotlin › play-audio-from-url-in-android-using-kotlin
Play Audio From URL in Android using Kotlin - GeeksforGeeks
July 23, 2025 - Many applications want to add different types of audio files to their android applications. These audio files are played using a media player within the android application. We can play audio files within the android application from different sources by playing audio from a web URL or by simply adding an audio file within our android application.
🌐
Stack Overflow
stackoverflow.com › questions › 45231524 › how-do-i-play-audio-file-from-url-in-android
How do i play audio file from url in android? - Stack Overflow
July 21, 2017 - try { mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setDataSource("your url"); mp.prepare(); mp.start(); } catch (Exception e) { // handle exception } ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... This question is in a collective: a subcommunity defined by tags with relevant content and experts. ... Stack Overflow migration maintenance Tuesday, April 1, 2025 from 13:30 UTC to...
Top answer
1 of 7
79

simple Media Player with streaming example.For xml part you need one button with id button1 and two images in your drawable folder with name button_pause and button_play and please don't forget to add the internet permission in your manifest.

public class MainActivity extends Activity {
private Button btn;
/**
 * help to toggle between play and pause.
 */
private boolean playPause;
private MediaPlayer mediaPlayer;
/**
 * remain false till media is not completed, inside OnCompletionListener make it true.
 */
private boolean intialStage = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.button1);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    btn.setOnClickListener(pausePlay);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private OnClickListener pausePlay = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub

        if (!playPause) {
            btn.setBackgroundResource(R.drawable.button_pause);
            if (intialStage)
                new Player()
                        .execute("http://www.virginmegastore.me/Library/Music/CD_001214/Tracks/Track1.mp3");
            else {
                if (!mediaPlayer.isPlaying())
                    mediaPlayer.start();
            }
            playPause = true;
        } else {
            btn.setBackgroundResource(R.drawable.button_play);
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
            playPause = false;
        }
    }
};
/**
 * preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
 * @author piyush
 *
 */

class Player extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog progress;

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        Boolean prepared;
        try {

            mediaPlayer.setDataSource(params[0]);

            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    intialStage = true;
                    playPause=false;
                    btn.setBackgroundResource(R.drawable.button_play);
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });
            mediaPlayer.prepare();
            prepared = true;
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            Log.d("IllegarArgument", e.getMessage());
            prepared = false;
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            prepared = false;
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            prepared = false;
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            prepared = false;
            e.printStackTrace();
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (progress.isShowing()) {
            progress.cancel();
        }
        Log.d("Prepared", "//" + result);
        mediaPlayer.start();

        intialStage = false;
    }

    public Player() {
        progress = new ProgressDialog(MainActivity.this);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        this.progress.setMessage("Buffering...");
        this.progress.show();

    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.reset();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}
2 of 7
35

Android MediaPlayer doesn't support streaming of MP3 natively until 2.2. In older versions of the OS it appears to only stream 3GP natively. You can try the pocketjourney code, although it's old (there's a new version here) and I had trouble making it sticky — it would stutter whenever it refilled the buffer.

The NPR News app for Android is open source and uses a local proxy server to handle MP3 streaming in versions of the OS before 2.2. You can see the relevant code in lines 199-216 (r94) here: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/PlaybackService.java?r=7cf2352b5c3c0fbcdc18a5a8c67d836577e7e8e3

And this is the StreamProxy class: http://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java?r=e4984187f45c39a54ea6c88f71197762dbe10e72

The NPR app is also still getting the "error (-38, 0)" sometimes while streaming. This may be a threading issue or a network change issue. Check the issue tracker for updates.

🌐
GeeksforGeeks
geeksforgeeks.org › android › how-to-play-audio-from-url-in-android
How to Play Audio from URL in Android? - GeeksforGeeks
July 23, 2025 - 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.
🌐
Better Programming
betterprogramming.pub › how-to-play-audio-and-video-files-in-your-android-app-15b846411cb2
How to Play Audio and Video Files in Your Android App | by Ng Wai Foong | Better Programming
November 9, 2020 - “The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. You can play audio or video from media files stored in your application’s resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs.”
🌐
Blogger
upadhyayjiteshandroid.blogspot.com › 2013 › 01 › android-playing-audio-from-web-url.html
Android Mobile Application Development: Android Playing Audio from a web url
January 31, 2013 - Please make a project with the name AudioDemo and make a main class with the name AudioDemo.java and have the following code package com.jitesh.audiodemo; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageButton; public class AudioDemo extends Activity implements MediaPlayer.OnCompletionListener { private static ProgressDialog progre
🌐
Stack Overflow
stackoverflow.com › questions › 24728941 › playing-audio-source-from-url-wont-work
android - Playing audio source from url won't work - Stack Overflow
static final String AUDIO_PATH= "http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/play.mp3"; public void playAudio(String url)throws Exception{ killMediaPlayer(); sound3=new MediaPlayer(); sound3.setAudioStreamType(AudioManager.STREAM_MUSIC); sound3.setDataSource(url); sound3.setOnPreparedListener(this); sound3.prepareAsync(); @Override public void onPrepared(MediaPlayer mp) { mp.start(); } try { playAudio(AUDIO_PATH); } catch (Exception e){ }