Reputation:
I made for some time an app with a media player. But after a number of updates, I realized that the space required for downloading the app got very big. I decided to change the method and make the players play the music with the internet from a URL link, but I have no idea how to manage that. I believe that I have to set mediaplayer.setDataSource and then put the link. But after that it doesn't work with my code. Does anyone know how to convert my code to work in this method?
A part of my current code:
mediaPlayer = MediaPlayer.create(this,R.raw.storm);
runnable = new Runnable() {
@Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
handler.postDelayed(this,500);
}
};
int duration = mediaPlayer.getDuration();
String sDuration = convertFormat(duration);
playerDuration.setText(sDuration);
btPlay.setOnClickListener(v -> {
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
handler.postDelayed(runnable, 0);
btPlay.setVisibility(View.GONE);
btPause.setVisibility(View.VISIBLE);
});
btPause.setOnClickListener(v -> {
mediaPlayer.pause();
btPlay.setVisibility(View.VISIBLE);
btPause.setVisibility(View.GONE);
handler.removeCallbacks(runnable);
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
if (b) {
mediaPlayer.seekTo((int) progress);
}
playerPosition.setText(convertFormat(mediaPlayer.getCurrentPosition()));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener(mp -> {
btPause.setVisibility((View.GONE));
btPlay.setVisibility(View.VISIBLE);
mediaPlayer.seekTo(0);
});
}
Upvotes: 2
Views: 2864
Reputation: 1595
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
Upvotes: 4