0

I built an app for randomizing my ringtones following "Create a Ringtone Randomizer on Android" tutorial on https://code.tutsplus.com, but when I run the app I get this error:

E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present

And the ringtones are not loaded in the ListView.

Here is my code:

mainactivity.java:

public class MainActivity extends AppCompatActivity {
private ListView list;
private ToggleButton toggle;
private List<Ringtone> ringtones;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toggle = (ToggleButton) findViewById(R.id.toggleButton);
    list = (ListView) findViewById(R.id.list_of_ringtones);

    ringtones = RingtoneHelper.fetchAvailableRingtones(this);

    initToggle();
    initList();

}

public void initToggle(){
    final SharedPreferences sharedPreferences = getSharedPreferences("Randomizer", Context.MODE_PRIVATE);
    boolean active = sharedPreferences.getBoolean("active", false);
    toggle.setChecked(active);

    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            sharedPreferences.edit().putBoolean("active", isChecked).apply();
        }
    });
}

public void initList(){
    ArrayAdapter<Ringtone> adapter = new ArrayAdapter<Ringtone>(this, android.R.layout.simple_list_item_1){
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            TextView item = (TextView) super.getView(position, convertView, parent);
            String title = ringtones.get(position).getTitle(MainActivity.this);
            item.setText(title);
            Log.v("MainActivity", title);
            return item;
        }
    };
    list.setAdapter(adapter);
}

}

ringreceiver.java

public class RingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
        String callState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(callState.equals(TelephonyManager.EXTRA_STATE_RINGING)){
            RingtoneHelper.changeRingtone(context);
        }
    }
}

}

ringtonehelper.java

public class RingtoneHelper {

public static List<Ringtone> fetchAvailableRingtones(Context context){
    List<Ringtone> list = new ArrayList<>();
    RingtoneManager rm = new RingtoneManager(context);
    rm.setType(RingtoneManager.TYPE_NOTIFICATION);

    Cursor ringtones = rm.getCursor();
    int count = ringtones.getCount();
    for(int i = 0; i <= count; i++){
        list.add(rm.getRingtone(i));
        Log.v("Ringtone Helper",String.valueOf(count));

    }
    return list;
}

public static void changeRingtone(Context context){
    SharedPreferences sharedPreferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE);
    if(!sharedPreferences.getBoolean("active", false)){
        return;
    }
    RingtoneManager rm = new RingtoneManager(context);
    Random random = new Random(System.currentTimeMillis());

    int n = random.nextInt(rm.getCursor().getCount());
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, rm.getRingtoneUri(n));
}

}

I know some other people have addressed this question but they got the error while using MediaPlayer, so I don't think their answers apply here.

Do you have any idea what is wrong? I would appreciate any suggestions.

Thank you

JediCate
  • 396
  • 4
  • 8

1 Answers1

0

So I solved the problem by getting the songs uri through the android MediaPlayer and I don't get that E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present error anymore. Solved one problem, but the app still does not work. So my next idea is to debug deeper into shared preferences.

JediCate
  • 396
  • 4
  • 8