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
Run Lint When Building Android Studio Projects
Javadoc in Eclipse Failing to Recognize Packages
Android Webview - Intercept Clicks
List View Items Changes Position When Scrolling Android
Image Size for All Screen Devices
Getting Permission Denial Exception
Android Viewpager Implementation
Gradle Sync Failed Could Not Find Constraint-Layout:1.0.0-Alpha2
Firebase Ontokenrefresh() Is Not Called
How to Setlayoutparams() for an Imageview
How to Reference an Asset in a Library Project
How to Check If "Android.Permission.Package_Usage_Stats" Permission Is Given
Android Studio Getslotfrombufferlocked: Unknown Buffer Error
How to Filter Listview Data When Typing on Edittext in Android
How to Get Profile Like Gender from Google Signin in Android
How to Get My Location Changed Event with Google Maps Android API V2