I want to play a music file which will be retrieved from a cloud storage service (such as Google drive or Mega.nz). I shared the file so anyone who have the link can access to it.
I'm using MediaPlayer
class to handle this playback. So when I tried a direct link something like this, it worked well. But when I tried with a link from Google Drive, such as this, it didn't work.
Here is some code which I used to play the music file:
MusicPlayerFragment class
public class MusicPlayerFragment extends Fragment {
private MusicPlayer musicPlayer;
boolean isPlaying;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_music_player, container, false);
final ImageButton buttonPlayPause = layout.findViewById(R.id.button_play_pause);
buttonPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isPlaying) {
musicPlayer.play();
buttonPlayPause.setImageResource(R.drawable.baseline_pause_24);
isPlaying = true;
}
else {
musicPlayer.pause();
buttonPlayPause.setImageResource(R.drawable.baseline_play_arrow_24);
isPlaying = false;
}
}
});
return layout;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
musicPlayer = new MusicPlayer();
musicPlayer.loadMedia("http://ssaurel.com/tmp/mymusic.mp3"); //work on physical device
//musicPlayer.loadMedia("https://drive.google.com/file/d/1Tj0a5f4dUMNnlPILr3vZzpPOwHKsP3Va/view?usp=sharing"); //doesn't work at all
}
}
MusicPlayer class
public class MusicPlayer{
private MediaPlayer mediaPlayer;
public void loadMedia(String url) {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
try {
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void play() {
if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
public void pause() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
public void reset() {
if (mediaPlayer != null) {
mediaPlayer.reset();
}
}
public void release() {
if (mediaPlayer != null) {
mediaPlayer.release();
}
}
}
I'm working with SQL Server too, which will store data of the music and URL to that music file to play. But since it didn't work so there are 2 things I'm wondering:
- 1st: Where am I wrong in here? In my opinion, I'm suspecting that I'm not using the
setDataSource(url)
method in the correct way. - 2nd: Is the URL of Google Drive in the right format to use? If it is not the right format to use with any (overloaded)
setDataSource()
method, then how can I store a music file with the right URL format it needs? (the right format like this, I think: "http://domain/path/audiofilename.mp3"). I'm accessing the database on local machine.