Question

Essentially the title says it all. I want to be able to retrieve the currently playing ringtone in the phonestatelistener class. One solution might be to simply retrieve DEFAULT URI from the RingtoneManager but what if there is a custom contact ringtone playing?

Obligatory code snippets for what I've tried so far. None of them return the ringtone. the IsPlaying is always false.

public static String getPlayingRingtone() {
    RingtoneManager m = new RingtoneManager(mCtx);
    Cursor rings = m.getCursor();
    for( int i = 0; i < rings.getCount(); i++ ) {
        Ringtone tone = m.getRingtone(i);
        if (tone.isPlaying() )
        Log.d("Playcallringtone", "**Is playing**" + tone.getTitle(mCtx));
    }
    return null;
}

Alternatively, I've tried:

    Log.d("Playcallringtone", RingtoneManager.getRingtone(mCtx,
          RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)).getTitle(mCtx) + " is playing");

My Phonestatelistener is as following:

private class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state,String number){
        switch(state){
        case TelephonyManager.CALL_STATE_IDLE:
                            // Do stuff for IDLE
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
                            // Do stuff of OFFHOOK
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            getPlayingRingtone();
                            // Do stuff for RINGING
            break;
        }
    } 
}
Was it helpful?

Solution

there is property in ContactsContract.ContactOptionsColumns : public static final String CUSTOM_RINGTONE that has URI for a custom ringtone associated with the contact. If null or missing, the default ringtone is used. So in your phone state listener when u fetch the number get the contact object and check its CUSTOM_RINGTONE property. If its null then fetch the default ringtone from RingtoneManager else you can get the customized ringtone for the contact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top