On Android, I’m having a problem trying to figure out which ringtone is actually playing (I’m not trying to detect the default ringtone, but the one actually playing as it may be different due to user setting a particular ringtone for a particular contact).
I’m using the Ringtone.isPlaying() function as I cycle through (successfully) all the available Ringtones from the RingtoneManager. However none of them ever returns true to Ringtone.isPlaying()! Anyone have a clue what I am doing wrong? Here is the code sample that is definitely being run whilst the ring is playing:
RingtoneManager rm = new RingtoneManager(this); // 'this' is my activity (actually a Service in my case)
if (rm != null)
{
Cursor cursor = rm.getCursor();
cursor.moveToFirst();
for (int i = 0; ; i++)
{
Ringtone ringtone = rm.getRingtone(i); // get the ring tone at this position in the Cursor
if (ringtone == null)
break;
else if (ringtone.isPlaying() == true)
return (ringtone.getTitle(this)); // *should* return title of the playing ringtone
}
return "FAILED AGAIN!"; // always ends up here
}
If you look at the source of
Ringtoneyou can see that theisPlaying()method only cares about that particular instance ofRingtone.When you call
getRingtone()fromRingtoneManager()it creates a newRingtoneobject (source). So this will not be the sameRingtoneobject used to play a sound when someone calls (ifRingtoneobjects are used to do that) soisPlaying()will always returnfalsein your case.isPlaying()will only ever returntrueif you have calledplay()on that specificRingtoneobject.As each application creates its own
MediaPlayerobjects I don’t think you can monitor which sounds other applications are currently playing.