Search code examples
javaandroidringtone

Exception : Setting filter columns should be done before querying for ringtones.(?)


I'm trying to List All Android Default (Ringtones,Alarm,Notifications) sounds. But I'm getting an exception, if I try to play one of the resource. Said I should set the type before querying for ringtones but I'm doing it... I don't get it.

I have on my ArrayList, I have some clues about why this's happening but if some one could help me understand of why this is happening...

Listing all Sounds

    RingtoneManager ringtoneMgr = new RingtoneManager(this);
    List<Uri> listRingtones = new ArrayList<Uri>();
    int ringTypes[] = {RingtoneManager.TYPE_ALARM,
                       RingtoneManager.TYPE_RINGTONE,
                       RingtoneManager.TYPE_NOTIFICATION};

    for (int ringType : ringTypes) {

        ringtoneMgr.setType(ringType);
        Cursor alarmsCursor = ringtoneMgr.getCursor();
        if (alarmsCursor.getCount() == 0 && !alarmsCursor.moveToFirst()) {
            return null;
        }

        while (!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
            listRingtones.add(
                       ringtoneMgr.getRingtoneUri(alarmsCursor.getPosition()));
        }

        alarmsCursor.close();
    }

Playing one of the sounds form the list:

    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.setDataSource(context, listRingtones.get(selected));
        final AudioManager audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (IOException e) {
        /*...*/
    }

Being selected the integer value corresponding to the selected sound in the list.

Listing the console log errors:

 FATAL EXCEPTION: main
: java.lang.RuntimeException: Unable to start activity ComponentInfo{AlarmReceiverActivity}: java.lang.IllegalStateException: Setting filter columns should be done before querying for ringtones.
:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
:   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
:   at android.app.ActivityThread.access$1500(ActivityThread.java:121)
:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
:   at android.os.Handler.dispatchMessage(Handler.java:99)
:   at android.os.Looper.loop(Looper.java:123)
:   at android.app.ActivityThread.main(ActivityThread.java:3701)
:   at java.lang.reflect.Method.invokeNative(Native Method)
:   at java.lang.reflect.Method.invoke(Method.java:507)
:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
:   at dalvik.system.NativeStart.main(Native Method)
: Caused by: java.lang.IllegalStateException: Setting filter columns should be done before querying for ringtones.
:   at android.media.RingtoneManager.setType(RingtoneManager.java:268)
:   at AlarmReceiverActivity.getAlarmUri(AlarmReceiverActivity.java:212)
:   at AlarmReceiverActivity.onCreate(AlarmReceiverActivity.java:83)
:   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
:   ... 11 more

Solution

  • Well after some investigation I figured out why is throwing this exception in my code.

    The problem is being caused because I was querying the different types of sounds from the RingtoneManager and this was happening because I'm using the one instance of RingtoneManager to query all types of sounds , so Once you query a type of sound you can't query for an other type with the same instance of RingtoneManager you need to use a different one , so I just create a new instance of the RingtoneManager each time I was querying for new sounds.

    Changing part of my code above , by this :

    List<Uri> listRingtones = new ArrayList<Uri>();
    int ringTypes[] = {RingtoneManager.TYPE_ALARM,
                       RingtoneManager.TYPE_RINGTONE,
                       RingtoneManager.TYPE_NOTIFICATION};
    
    for (int ringType : ringTypes) {
    
        RingtoneManager ringtoneMgr = new RingtoneManager(this);
        ringtoneMgr.setType(ringType);
        Cursor alarmsCursor = ringtoneMgr.getCursor();
        if (alarmsCursor.getCount() == 0 && !alarmsCursor.moveToFirst()) {
            return null;
        }
    
        while (!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
            listRingtones.add(
                       ringtoneMgr.getRingtoneUri(alarmsCursor.getPosition()));
        }
    
        alarmsCursor.close();
    }
    

    This solve my problem, I hope it helps.