RingtoneManager returning null ringtone
Asked Answered
F

2

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?

Finnish answered 23/3, 2011 at 22:54 Comment(2)
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?Motherly
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?Finnish
S
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.

Sama answered 13/1, 2013 at 10:31 Comment(1)
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.Beneficiary
H
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

Hollins answered 12/1, 2014 at 15:43 Comment(2)
thanks a lot!!! I spent hours with trying to solve this problem, until I finally found your post!Mauldin
Thanks a lot! I didn't spend hours trying to solve this issue, because I found your post!Doloroso

© 2022 - 2024 — McMap. All rights reserved.