Kartik Sharma
Kartik Sharma

Reputation: 723

Select song/ringtone intent

What I am trying to do is create alarm like application. Main coding is done. Currently it plays a song which is added in my apk (raw folder).

I want to add a feature where a user can choose his/her own song as alarm sound. It can be a file from SD card, internal or a ringtone from the tunes Android provide.

I am playing sound by using MediaPlayer.

Upvotes: 1

Views: 1804

Answers (1)

KickAss
KickAss

Reputation: 4280

This is in an onClick handler of a button labeled "set ringtone" or something similar:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);

And this code captures the choice made by the user:

 @Override
 protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent)
 {
     if (resultCode == Activity.RESULT_OK && requestCode == 5)
     {
          Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

          if (uri != null)
          {
              this.chosenRingtone = uri.toString();
          }
          else
          {
              this.chosenRingtone = null;
          }
      }            
  }

Also, I advise my users to install the "Rings Extended" app from the Android Market. Then whenever this dialog is opened on their device, such as from my app or from the phone's settings menu, the user will have the additional choice of picking any of the mp3s stored on their device, not just the built in ringtones.

Upvotes: 4

Related Questions