Streaming Audio from a Url in Android Using Mediaplayer

how to play audio file from url in android

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;
}

How can I make the media player play the music with the URL link?

Please try the below code to play music via URL

Kotlin:

 val mediaPlayer = MediaPlayer()
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
try {
mediaPlayer.setDataSource(yourSongUrl)
mediaPlayer.prepare()
mediaPlayer.start()
} catch (e: IOException) {
e.printStackTrace()
}
Log.v(TAG,"Music is streaming")

Java:

 MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mediaPlayer.setDataSource(yourSongUrl);
mediaPlayer.prepare();
mediaPlayer.start();

} catch (IOException e) {
e.printStackTrace();
}
Log.v(TAG,"Music is streaming")

If you target the API level 26 or above theb setAudioStreamType() is deprecated. So try the below code:

 //java
mediaPlayer.setAudioAttributes(
new AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
//Kotlin
mediaPlayer.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)

Note: Add Internet permission in manifest.xml file

<uses-permission android:name="android.permission.INTERNET" />

last but not least, you have to wait for the music to play because it's playing the audio from the URL so it takes time and also depends on the user internet speed

Android MediaPlayer audio streaming - which URL to use?

Could you write a wrapper function which downloads the .pls file from the URL you define, and then extracts the URLs from it? This SO question seems to address doing just that. There's a bit of code embedded in the answer there that probably does what you want.

I can't play audio from URL by MediaPlayer Android

Your URL is returning html. MediaPlayer doesn't know anything about playing HTML pages. Just because it ends in .mp3 doesn't mean it is an mp3.

You can check this easily by just visiting this page in a browser:

http://ciacho090.wrzuta.pl/audio/31h2JLMRCE7/eminem_soldier.mp3

Here's a "real" mp3 you can test with:

http://ia700200.us.archive.org/1/items/testmp3testfile/mpthreetest.mp3

In short: make sure you are passing a url that returns an mp3 or other supported audio format.

How to stream music from url with Java - Android

Intent is using only for sending some data between activities/services and system. It won't play the music. It don't do anything except saying to some activity what to do. You need the mechanism which will play your multimedia stream. You should use MediaPlayer class for playing multimedia inside your application.

Here's some tutorial, how to play music from stream: http://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-internet/



Related Topics



Leave a reply



Submit