簡體   English   中英

Android - 在啟動屏幕上禁用聲音

[英]Android - Disabling sound on start up screen

我的應用程序有一個啟動畫面,在啟動時播放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();

}

使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM