How to get current ringtone in Android?
Asked Answered
M

6

12

I have found lots of examples how to get default ringtone. Something like that:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(),  alert);
         ringtone.setStreamType(AudioManager.STREAM_RING);
         ringtone.play();

But I want to get the actual current ringtone, that was set by the user.

Miocene answered 19/3, 2014 at 10:41 Comment(0)
W
20

I was struggling with this issue as well, so the solution is:

Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getActivity().getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
Ringtone defaultRingtone = RingtoneManager.getRingtone(getActivity(), defaultRingtoneUri);
Whomever answered 25/6, 2014 at 16:24 Comment(2)
what is getActivity() ?Koziol
The current Activity instance, which is used in Fragments, if you are in an Activity, use YourActivity.this instead.Washroom
R
1

I've also searched a lot for your problem which I also had. At last got solution from this itself.

Use below code in the button for ringtone intent.

public void pickRingtone(View view) {
        // TODO Auto-generated method.stub

        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");

    // for existing ringtone
        Uri urie = RingtoneManager.getActualDefaultRingtoneUri(
                getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, urie);

    startActivityForResult(intent, 5);
}
Rather answered 11/4, 2014 at 8:38 Comment(1)
This still didn't answer my question. I just simply want to grab a URI for the currently selected ringtone (by a user). I don't need any Ringtone Picker or anything like that. I just need to get a uri and play it when I need it.Miocene
I
1

This is the extension of dgabor's answer. Just add defaultRingtone.play(); to play the ringtone.

Uri defaultRintoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
Ringtone defaultRingtone = RingtoneManager.getRingtone(getApplicationContext(), defaultRintoneUri);
defaultRingtone.play();
Inefficient answered 2/8, 2016 at 13:1 Comment(0)
T
1

Answer by Alexander The Great was correct. But, I simplified it.

Uri currentRingtonUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
Thirza answered 4/8, 2021 at 14:45 Comment(0)
E
0

Try with:

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI);
Estimative answered 19/3, 2014 at 10:50 Comment(1)
This won't work, because RingtoneManager.getDefaultUri accepts only INT parameter, while RingtoneManager.EXTRA_RINGTONE_EXISTING_URI is a String.Miocene
C
0

try this:

uri default_ringtone_uri = Settings.System.DeFAULT_RINGTONE_URI

Ringtone ringtone = RingtoneManager.getRingtone(context, uri).play();
Casar answered 25/3, 2018 at 9:11 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Schipperke

© 2022 - 2024 — McMap. All rights reserved.