Pick songs from SD card on Android and play it

Geetanjali picture Geetanjali · Aug 12, 2011 · Viewed 16.4k times · Source

I have songs in the Music folder on the SD card.

And in my app I want to pick a song from SD card from the list of music files, and when I choose any song, then using the Play button it should be played. How can I do that?

I have already done it using content resolver and got a list of songs. Is there a way to do this using intents to pick from stored songs the SD card?

I have done this by both the ways. Just check it.

Answer

Geetanjali picture Geetanjali · Aug 16, 2011

Finally I got my answer.

Code using intents is:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    super.onOptionsItemSelected(item);
    System.gc();
       Intent intent = new Intent();
       intent.setAction(android.content.Intent.ACTION_VIEW);
       Uri data = Uri.parse("file:///sdcard/Music");
       String type = "audio/mp3";
       intent.setDataAndType(data, type);
       startActivityForResult(intent, Pick_song);
    return true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode)
    {
        case Pick_song : if (resultCode == RESULT_OK)
        {
            muri = getIntent().getData();
            //String ringTonePath = muri.toString();

            if (muri != null)
            {
                try
                {
                    mMediaPlayer.start();
                    mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
                    {
                           @Override
                        public void onCompletion(MediaPlayer mp)
                           {
                               mp.release();
                            mp = null;
                        }
                    });
                }
                catch (Exception exception)
                {
                    exception.printStackTrace();
                }
            }
        }