2

this is the intent which opens a chooser from the music app

public void launchMusicPlayer(View view) {
        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,1);
    }

and below is the onActivityResult code to try and obtain the song , but the problem is that the test button when pressed, does not play anything and as i am new to coding i don't know what i am doing wrong.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == 1) {
        final MediaPlayer testSong = new MediaPlayer();
        Uri songUri = data.getData();

            try {
                testSong.setDataSource(this, songUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
        testSong.prepareAsync();

        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testSong.start();
            }
        });
    }
}

1 Answers1

1

The issue maybe because prepareAsync() is asynchronous, so the MediaPlayer may not be ready when you call start(). There are 2 ways to fix this:

  1. Use prepare() instead of prepareAsync()

    test.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            testSong.prepare(); 
            testSong.start();
        }
    });
    
  2. call start() method inside setOnPreparedListener()

    test.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            testSong.prepareAsync(); 
            testSong.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    
                @Override
                public void onPrepared(MediaPlayer player) {
                    player.start();
                }
            }
    });
    

Refer to the documentation here http://developer.android.com/reference/android/media/MediaPlayer.html

Tony Vu
  • 4,251
  • 3
  • 31
  • 38
  • Thank you. I tried your method however still the same issue. So I made an onClickListener for the button in onCreate of activity to play a specific song from the raw folder and this works. But if I pick a song from the chooser then press the test button it still plays the song from the raw folder meaning it is not even reading the onActivityResult so what should I do? @Tony Vu – Mike Napoli Aug 12 '15 at 04:26
  • Oh that will be different issue from your original post. Anyway, I think the problem is with the Uri returned by data.getData(). It may need some processing before passing to setDataSource(). You can try the methods as recommended by this thread http://stackoverflow.com/questions/17042308/select-a-music-file-to-play-with-mediaplayer – Tony Vu Aug 12 '15 at 04:46
  • Thank you @Tony Vu I got it to work with the last answer within that question – Mike Napoli Aug 15 '15 at 07:03