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