Android detect which ringtone is actually playing (Ringtone.isPlaying problem)

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();
     for (int i = 0; ; i++)
            Ringtone ringtone = rm.getRingtone(i);  // get the ring tone at this position in the Cursor
            if (ringtone == null)
        else if (ringtone.isPlaying() == true)
                return (ringtone.getTitle(this));   // *should* return title of the playing ringtone
    return "FAILED AGAIN!"; // always ends up here

Asked by: Julian194 | Posted: 25-01-2022

Answer 1

If you look at the source of Ringtone you can see that the isPlaying() method only cares about that particular instance of Ringtone.

When you call getRingtone() from RingtoneManager() it creates a new Ringtone object (source). So this will not be the same Ringtone object used to play a sound when someone calls (if Ringtone objects are used to do that) so isPlaying() will always return false in your case.

isPlaying() will only ever return true if you have called play() on that specific Ringtone object.

As each application creates its own MediaPlayer objects I don't think you can monitor which sounds other applications are currently playing.

Answered by: Kevin223 | Posted: 26-02-2022

Similar questions

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android