简体   繁体   中英

how to play the music one time in …?

I have an checkbox in share preferences an want to: When the checkbox is true, this song play on the activity1 . When opening the list item's just start activity1 . And in activity1 I have an class that check the sharepreferences and is play the song , so that:

When opening the every list item's it play song , and play many song when opening every list item. How to play just one song in this activity.

it's call for every list item :

protected void All()
{
    boolean music = sharedpreferences.getBoolean("musictype", false);
    if (music) { mp1.start(); } else { mp1.stop();}      
}                           
public class Activity1 extends Activity {
    boolean hasPlayed;
    boolean shouldPlayMusic;

    public void onCreate(Bundle savedInstanceState) {
        hasPlayed = false;
        SharedPreferences prefs = PreferenceManager.getDefaultSharePreferences();
        shouldPlayMusic = prefs.getBoolean("musictype", false);
        //other initialization
    }

    protected void All() {
        if(shouldPlayMusic && !hasPlayed) {
            //Should check state of MediaPlayer too
            mp1.start();
            hasPlayed = true;
        }
    }
}

Basically something like that. Keep a boolean flag of whether you've played once already, and check that as well as the preference value before you play it. Once you've played it, set the flag so that it won't play any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM