简体   繁体   English

一次如何播放音乐?

[英]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 . 我想在共享首选项中有一个复选框:当该复选框为true时,这首歌曲将在activity1上播放。 When opening the list item's just start activity1 . 打开列表项时,只需启动activity1 And in activity1 I have an class that check the sharepreferences and is play the song , so that: activity1我有一个用于检查sharepreferences并播放歌曲的类,因此:

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. 播放后,设置标志,使其不再播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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