In this code, i made a preference check box with key "music" in xml folder.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://ift.tt/nIICcg" >
<CheckBoxPreference
android:title="Music"
android:key="music"
android:defaultValue="true"
android:summary="Check/Uncheck this to switch on/off sound"
/>
</PreferenceScreen>
If this checkbox is checked , then my mediaplayer will start playing . If this is unchecked , it will not start. But the music is not stopping immediately after unchecking it.
What i want to implement is, if i uncheck the checkbox in preferences the music should stop immediatly
Below is the code i wrote in my MainActivity , i also used this code in onPause() method. But nothing works.
Hope you help me guys.. Thanks in advance..
MediaPlayer song;
song= MediaPlayer.create(this, R.raw.bg);
song.setLooping(true);
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Boolean music=prefs.getBoolean("music", true);
if(music == true)
song.start();
Aucun commentaire:
Enregistrer un commentaire