dilipkaklotar
dilipkaklotar

Reputation: 1469

How to get Notification tones uri in android

I want to get the notification tones URI from my android device.

how can get get the uri of particular tones which i set as notification tone ?

Upvotes: 2

Views: 2865

Answers (3)

Brian Ombisa
Brian Ombisa

Reputation: 51

I used the format below and it worked for me. I have the tone in my resources raw folder. It has the message and the notification icon but the tone is specific. Try this I think it will help you out.

Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle("Your Title")
                .setContentText(Html.fromHtml(String.format("New message", messageThreadArrayList.size() > 1 ? "s" : "")))
                .setContentIntent(messagePendingIntent)
                .setLargeIcon(largeIcon)
                .setAutoCancel(true)
                .setSound(Uri.parse(String.format(Locale.ENGLISH, "android.resource://%s/%d", getPackageName(), R.raw.notification_tone)))
                .setSmallIcon(R.mipmap.ic_olla_logo_action_bar)
                .setStyle(inboxStyle.setBigContentTitle("Your Title"))
                .addAction(R.mipmap.notification_icon, "Reply", messagePendingIntent)
                .build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(Configuration.NOTIFICATION_ID2, notification);
    }

Upvotes: 0

user647826
user647826

Reputation:

Just copy/pasting some code from one of my apps that does what you are looking for.

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: 3

Lukas Knuth
Lukas Knuth

Reputation: 25755

I guess the jingle you'd like to play ships with your application?

If you want your Notification to play a custom sound you have two options:

  1. Using a direct Uri (!= URI!!)
  2. Using the internal MediaStore's ContentProvider (can be usefull if you want the user to select his own tune).

Examples can be found in the Docs.

So, lets assume your jingle is located in your applications res/raw-folder, you would to this (see this question: Trying to play video from raw folder (VideoView)):

Uri jingle = Uri.parse("android.resource://com.pac.myapp/raw/jingle.mp3");

If your file is located on the phones SD-Card, you can do:

Uri jingle = Uri.parse("file:///sdcard/jingle.mp3");

Upvotes: 1

Related Questions