I am trying to play a ringtone selected by the user.
When I use this code:
RingtoneManager ringtoneManager = new RingtoneManager(this);
Uri uri = ringtoneManager.getRingtoneUri(position);
if(uri == null) {
Log.d("ALARM", "uri is null");
}
try {
mediaPlayer.setDataSource(getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
mediaPlayer.start();
} catch (Exception e) {
// Handle exception
}
I get "uri is null" and nothing happens, however if I put in an empty for loop beforehand like this:
for(int i = 0; i < ringtoneManager.getCursor().getCount(); ++i) {
//WTF
}
RingtoneManager ringtoneManager = new RingtoneManager(this);
Uri uri = ringtoneManager.getRingtoneUri(position);
if(uri == null) {
Log.d("ALARM", "uri is null");
}
try {
mediaPlayer.setDataSource(getApplicationContext(), uri);
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
mediaPlayer.start();
} catch (Exception e) {
// Handle exception
}
The uri is not null and the ringtone plays...
Can anyone explain what is going on here, and what I am supposed to do to avoid this hack?
Thanks