0

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.
halfer
  • 19,824
  • 17
  • 99
  • 186
Chocolatto
  • 63
  • 7
  • Since gdrive doesn't give you a direct link to the file, I guess you'll need to [download it](https://stackoverflow.com/questions/29343041/how-to-download-mp3-file-from-google-drive-in-android) before playing the file – Ricardo A. Jun 06 '19 at 18:15
  • [Maybe this can help](https://stackoverflow.com/questions/29596614/android-stream-video-from-google-drive) – Ricardo A. Jun 06 '19 at 18:18

1 Answers1

0

You have the answer hidden in your question itself. When you're passing the direct download link to the music, it works, meaning, you need to use something that will be able to directly download the file.

When you give the Google Drive link, you don't give the complete address of the file, instead, you go on the page which tells you to manually download the file.

You should use something like FirebaseStorage to store the songs, so you'll be able to get the direct download links of those songs.

Vedprakash Wagh
  • 3,595
  • 3
  • 12
  • 33