• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps android access to ringtones list in settings

tht13

Lurker
Hi I'm a novice at android, and I'm writing a basic sudoku app, (as written in "Hello, Android" by Ed Burnette)
I've begun experimenting and a feature id like to add is being able to set a file from the ring tones list on SD card as the playing music, what would be the xml code to access this that I would use?
 
Your(at least mine) system-sounds is located here: /system/media/audio/

This is not going to work in the emulator, because the emulator doesn't have any sounds!

You can use this code to start the sound hardcoded:
Code:
MediaPlayer mp = MediaPlayer.create(this, Uri.fromFile(new File("/system/media/audio/ringtones/Fire_Fly.mp3")));
			mp.setLooping(true);
			mp.start();

Or you can use this xml code to pick a ringtone:
Code:
<RingtonePreference
            android:name="Ringtone Preference"
            android:ringtoneType="notification"
            android:showSilent="false"
            android:summary="@string/preferences_notification_sound_summary"
            android:title="@string/preferences_notification_sound_title"
            android:key="sound" />

How to make the selected ringtone in to a Uri:
Code:
Uri.parse(PreferenceManager.getDefaultSharedPreferences(c).getString("sound", "content://settings/system/notification_sound"));
 
ok if im at a loss ill use that, but what i would like to do to give the user more option is let them choose a file from which they've added on their SD card, how would I get the directory to fill the list without know what the contents are (i.e when you select default ringtone in settings, the list adds all files in the ringtones directory to the list)
 
i've placed in the xml code for the ringtone, however i am unfamiliar with uri,

from the book ive got a Prefs.java page written and so far it has boolean choices (check boxes)
HTML:
private static final String OPT_MUSIC = "music";
    private static final boolean OPT_MUSIC_DEF = true;
and then
HTML:
public static boolean getMusic(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_MUSIC, OPT_MUSIC_DEF);
    }
now my problem with doing this for "sound" is that i dont know what word to use instead of boolean, since its a list selection, any suggestions?
 
If you look at my post at the bottom i have given an example of how to get the selected rintone name from the list. It is saved as String.
 
Uri | Android Developers

Okey. So lets say you have chosen a ringtone in your preferences. And you go to the screen where the ringtone should play. Use this code:
Code:
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(PreferenceManager.getDefaultSharedPreferences(c).getString("sound", "content://settings/system/notification_sound"));
			mp.setLooping(true);
			mp.start();
 
yes i tried that, but the way the book told me to write the code is a bit different
it told me to make a void called "play" in music.java
Code:
public static void play(Context context, int resource) {
            stop(context);
            // start music only if not disables in preferences
            if (Prefs.getMusic(context)) {
                mp = MediaPlayer.create(context, resource);
                mp.setLooping(true);
                mp.start();
            }
        }
and then link to that with selecting the file in the main menu, Sudoku.java
Code:
@Override
    protected void onResume() {
        super.onResume();
        Music.play(this, R.raw.tnt);
    }
 
I have the same book, and in that app you have a music file in your apk file that you reference with "resource"(R.raw.tnt). But when you want to get a music file which is outside of your app, you cant reference it the same way. Then you have to use a Uri.
 
is there no way to have something like getSound?
and then would i get rid of the music.java? and place the music player code in the "onResume" void?
 
Sure. You meen something like this?

Code:
public void getSound()
{
    String file = PreferenceManager.getDefaultSharedPreferences(this).getString("sound", "content://settings/system/notification_sound");
    MediaPlayer mp = MediaPlayer.create(this, Uri.parse(file));
    mp.setLooping(true);
    mp.start();
}
 
ok i entered that code into Sudoku.java
and then replaced r.raw.tht with getSound(), it asked to change the definition play in music,java to (context, object) not (context, int)

Code:
public void getSound()
    {
        String file = PreferenceManager.getDefaultSharedPreferences(this).getString("sound", "content://settings/system/notification_sound");
        MediaPlayer mp = MediaPlayer.create(this, Uri.parse(file));
        mp.setLooping(true);
        mp.start();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Music.play(this, getSound());
    }
however it is giving me underline on Uri and play,
is says Uri cannot be resolved, and for play it says "The method play(Context, Object) in the type Music is not applicable for the arguments (Sudoku, void)"
the java for music is
Code:
public static void play(Context context, Object object) {
            stop(context);
            // start music only if not disables in preferences
            if (Prefs.getMusic(context)) {
                mp = MediaPlayer.create(context, (Uri) object);
                mp.setLooping(true);
                mp.start();
            }
        }
ive never had any experience with java, nor am i understanding much of this...
 
Back
Top Bottom