1

I am new to android. I am writing a small application that reads files from the SD card. It picks a song from the SD card and then plays that song as a ring tone.

This is the code that plays the song:

MediaPlayer mp=MediaPlayer.create(Alarm.this, R.raw.airtel_new);
mp.start();

Instead of "R.raw.airtel_new" I would like to use the URI or the URL of a particular song that I have selected from the SD card. Could any one help me?

Shlublu
  • 10,917
  • 4
  • 51
  • 70
user903704
  • 75
  • 1
  • 6
  • Its easy, This link will be helpful to u. http://stackoverflow.com/questions/7035999/androidpick-songs-from-sd-card-and-play-it/7036162#7036162 – Randroid Aug 29 '11 at 09:55

1 Answers1

1

The root directory of your SDCard (where it is mounted) is known by Environment.getExternalStorageDirectory().getAbsolutePath().

So this allows you picking the file /music/yourfile.ext on the SD:

final String rootDir  = Environment.getExternalStorageDirectory().getAbsolutePath();
final String yourFile = rootDir + "/music/yourfile.ext";

final MediaPlayer mp = MediaPlayer.create(Alarm.this, "file://" + yourfile);

More details on this method are available here.

Shlublu
  • 10,917
  • 4
  • 51
  • 70