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

1

4

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
}
Dactylo answered 19/1, 2010 at 9:38 Comment(0)
E
6

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.

Eos answered 19/1, 2010 at 10:13 Comment(2)
Thanks Dave, That makes perfect sense now. Darn, that's a shame. I'm trying to figure out which ringtone is actually playing when the phone rings. Guess I'll poke around and see if I can get a handle to the MediaPlayer object or if I can deduce which ringtone is playing by querying the contacts database vs. the incoming number or something. Cheers, CliveDactylo
Did you by any chance find a way to detect which ringtone is playing at the moment?Costrel

© 2022 - 2024 — McMap. All rights reserved.