How I get the default ringtone list in android on programmatically?
Asked Answered
L

4

5

I am android developer and want to create a android apps.In this apps ,it needed to show the android default ringtone list.But i can not show it.How to get the android default ringtone list .

Please help to me.

Ludwog answered 11/9, 2013 at 3:29 Comment(0)
E
12

I have upvote Yuichi's answer but it did not completely work for me. For each ringtone I was getting same URI (different title thought). Following code worked for me -

public void listRingtones() {
  RingtoneManager manager = new RingtoneManager(this);
  manager.setType(RingtoneManager.TYPE_RINGTONE);
  Cursor cursor = manager.getCursor();
  while (cursor.moveToNext()) {
    String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
    Uri ringtoneURI = manager.getRingtoneUri(cursor.getPosition());
    // Do something with the title and the URI of ringtone
  }
}

You can see the working app on playstore.

Edytheee answered 2/5, 2015 at 19:43 Comment(4)
Only this code got me the full Uri of the ringtone including the name..Thanks!Chrisman
@Aniket Thakur How can i get the ringtone URI of manually added ringtone because with this code i am getting only system ringtone list? please helpBiblicist
How to get nameBiblicist
Don't forget cursor.close(); after loopCuman
F
3

Get a List of All ringTones with default ringtones in a list :

public static Map<String, Uri> getRingtones(Activity activity) {
    RingtoneManager manager = new RingtoneManager(activity);
    manager.setType(RingtoneManager.TYPE_RINGTONE);
    Cursor cursor = manager.getCursor();

    Map<String, Uri> list = new LinkedHashMap<>();
    String title = "Set to Default";
    Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(activity, RingtoneManager.TYPE_RINGTONE);
    list.put(title, defaultRingtoneUri); // first add the default, to get back if select another 

    while (cursor.moveToNext()) {
      String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
      Uri notificationUri = manager.getRingtoneUri(cursor.getPosition());

      list.put(notificationTitle, notificationUri);
    }



    return list;
  }

Then Create a dialog for select :

public static void getRingtonesDialog(Activity activity, Collection<String> items, int selected, DialogInterface.OnClickListener itemClickListener, DialogInterface.OnClickListener okClickListener, final DialogInterface.OnClickListener cancelClickListener) {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(activity);
    builderSingle.setTitle("Select Ringtone");
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(activity, android.R.layout.select_dialog_singlechoice);
    arrayAdapter.addAll(items);

    builderSingle.setNegativeButton(android.R.string.cancel, cancelClickListener);

    builderSingle.setPositiveButton(android.R.string.ok, okClickListener);

    builderSingle.setSingleChoiceItems(arrayAdapter, selected, itemClickListener);

    builderSingle.setCancelable(false);

    builderSingle.show();
  }

Then Select And Play them to test : (Here AppSettings settings is SharedPreferences in your App ) :

Map<String, Uri> mRingtonesMap;
    private Uri mLastSelectedRingtone = null;
    private String mLastSelectedRingtoneName = "Default";
    private MediaPlayer mMediaPlayer;
    AppSettings settings;
    TextView mRingtone ;

in Oncreate :

mRingtone = (TextView)findViewById(R.id.mRingtone);
        settings = AppSettings.getInstance(this);
        mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
                settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));
        mRingtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setupRingtoneSelection();
            }
        });

Then :

 private void setupRingtoneSelection() {
        mRingtonesMap = AlarmUtils.getRingtones(SelectionsActivity.this);
    mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
            settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));

    mRingtone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final List<String> ringtoneKeys = new ArrayList<>(mRingtonesMap.keySet());
            String ringtoneUri = settings.getString(AppSettings.Key.SELECTED_RINGTONE, "");
            int selected = -1;
            for (int i = 0; i < ringtoneKeys.size(); i++) {
                String ring = ringtoneKeys.get(i);
                if (mRingtonesMap.get(ring).toString().equalsIgnoreCase(ringtoneUri)) {
                    selected = i;
                    break;
                }
            }

            AlarmUtils.getRingtonesDialog(SelectionsActivity.this, ringtoneKeys, selected, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { //item selected
                    String name = ringtoneKeys.get(which);
                    mLastSelectedRingtoneName = name;
                    mLastSelectedRingtone = mRingtonesMap.get(name);

                    try {
                        if (mMediaPlayer == null) {
                            mMediaPlayer = new MediaPlayer();
                        } else {
                            mMediaPlayer.stop();
                            mMediaPlayer.reset();
                        }
                        mMediaPlayer.setDataSource(SelectionsActivity.this, mLastSelectedRingtone);
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mMediaPlayer.setLooping(false);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                    } catch (IOException ioe) {
                        //do nothing
                    }

                }
            }, new DialogInterface.OnClickListener() { //ok
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mMediaPlayer != null) {
                        mMediaPlayer.stop();
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                    }
                    settings.set(AppSettings.Key.SELECTED_RINGTONE, mLastSelectedRingtone.toString());
                    settings.set(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName);
                    //Update the textview
                    mRingtone.setText(getString(R.string.set_alarm_select_ringtone,
                            settings.getString(AppSettings.Key.SELECTED_RINGTONE_NAME, mLastSelectedRingtoneName)));
                    dialog.dismiss();
                }
            }, new DialogInterface.OnClickListener() { //cancel
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (mMediaPlayer != null) {
                        mMediaPlayer.stop();
                        mMediaPlayer.release();
                        mMediaPlayer = null;
                    }
                    dialog.dismiss();
                }
            });
        }
    });
}

@Override
protected void onDestroy() {
    if (mMediaPlayer != null) {
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
        }

        mMediaPlayer.release();
        mMediaPlayer = null;
    }

    mRingtone.setOnClickListener(null);

    super.onDestroy();
}

You can set and change ringtone, and after you want, can get back to default.

Flagellate answered 30/10, 2020 at 23:36 Comment(0)
T
0

This question/answer should provide what you're after: How to bring up list of available notification sounds on Android

You'll just need to swap out the RingtoneManager.TYPE_NOTIFICATION for RingtoneManager.TYPE_RINGTONE

Tsana answered 11/9, 2013 at 3:33 Comment(0)
N
0

You can get the list of Ringtones using RingtoneManager

public void listRingtones() {
  RingtoneManager manager = new RingtoneManager(this);
  manager.setType(RingtoneManager.TYPE_RINGTONE);
  Cursor cursor = manager.getCursor();
  while (cursor.moveToNext()) {
    String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
    String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);
    // Do something with the title and the URI of ringtone
  }
}
Norvun answered 11/9, 2013 at 3:41 Comment(4)
For the record you will need <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> for this.Edytheee
@Yuichi Araki How can i get the ringtone URI of manually added ringtone because with this code i am getting only system ringtone list? please helpBiblicist
I got this error: sql Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (no such column: is_ringtone_theme (code 1): , while compiling: SELECT _id, title, "content://media/internal/audio/media", title_key FROM audio WHERE ((is_alarm=1 or is_notification=1 or is_ringtone=1) and _size!=0 and is_ringtone_theme!=1 and is_notification_theme!=1 and is_alarm_theme!=1) ORDER BY title_key) on galaxy tab 10.1 2016 android 8.1Materially
cursor.getString(RingtoneManager.URI_COLUMN_INDEX); will not be a uri for the tuneCuman

© 2022 - 2024 — McMap. All rights reserved.