10

I'm using RingtoneManager, and apparently on some phones it returns null all of the time. I know it returns null if the sound is silent or it cant find the tone. Why would null be returned if the sound is on and there is a tone? The code works on my nexus s....

Here is what I am using:

Ringtone ringtone;
ringtone = RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
if(ringtone == null)
{
    Log.d("Debug", "ringtone is null");
}
else
{
    ringtone.play();
}

I have all permissions enabled to play ringtone (it works on my phone) and even have SD card permissions enabled just in case the ringtone is on the SD card. Any thoughts?

Julian
  • 20,008
  • 17
  • 77
  • 108
atomicbaum
  • 311
  • 1
  • 3
  • 6
  • According to the source code for RingtoneManager, the only case in which getRingtone returns null is when the ringtone at the given URI can't be opened. It should also put a line with `"Failed to open ringtone " + ringtoneUri` in logcat. Are you seeing such lines in your logcat output? – Jason Plank Nov 10 '11 at 16:19
  • Not on mine, but some users are complaining about the ringtone not firing. Before I had the NULL check it was crashing because of a dereferenced pointer. The only think I can assume is that it could not get a URI... But I don't know why it wouldn't be able to do that when they have a valid ringtone. Maybe a resource lock? – atomicbaum Dec 01 '11 at 23:33

2 Answers2

10

I just fall into what the problem is. If the user have "Silent" like notification sound the function:

RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))

returns null. And that explains why only some users find this issue.

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
  • This answer saved me some hours. It turns out the AVD I was using has all the sound settings on silent across the board. Then I read the above answer and found that the app works on my actual phone. – Lori Oct 30 '13 at 18:08
10

If you copy a costum sound onto your phone that is stored on the external storage, the RingtoneManager is not able to open it unless your app has the permission to access the external storage.

add missing permission to your manifest file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Took me some time to figure this out, while not getting the correct title of costum ringtones some user had on their devices

kirmandi
  • 116
  • 1
  • 5