Android - 在启动屏幕上禁用声音

时间:2012-06-22 11:16:42

标签: java android audio soundpool

我的应用程序有一个启动画面,可以在启动时播放mp3剪辑。

我想通过我的应用程序的设置菜单为用户提供禁用/启用声音的选项。我该如何实现这一点,以便应用程序每次打开应用程序时都会记住用户的偏好。

请参阅下面的代码了解声音。

public class Splash extends SherlockActivity {

SoundPool sp;
int explosion = 0;
MediaPlayer mp;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    getSupportActionBar().hide();

    setContentView(R.layout.splash);

    sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    explosion = sp.load(this, R.raw.soundfile, 1);

    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(5000);

                if (explosion != 0)
                    sp.play(explosion, 1, 1, 0, 0, 1);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Intent openMenu = new Intent(
                        "ttj.android.t3w.STARTINGPOINT");
                startActivity(openMenu);
            }

        }
    };
    timer.start();

}

1 个答案:

答案 0 :(得分:2)

使用SharedPreferences存储和检索它:http://developer.android.com/reference/android/content/SharedPreferences.html

示例:http://developer.android.com/guide/topics/data/data-storage.html#pref

public static final String PREFS_NAME = "MyPrefsFile";

//retrieve
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
//use silent

//store
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
editor.commit();