java - Android - 禁用启动屏幕上的声音

标签 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();

}

最佳答案

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

关于java - Android - 禁用启动屏幕上的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11155151/

相关文章:

java - 使用 Java 访问 OPC 数据

java - java.lang.NoClassDefFoundError:在hadoop集群上运行Mahout示例

java - 需要通过java查询tile38

java - 从 Log4j2 XML 配置中以编程方式获取属性

html - Android:如何从特定网页检索有问题的数据?

Android:如何创建这样的自定义编辑文本框?

android - 广播接收器中的时区示例

c# - 如何获得mp3轨道的持续时间?

Android:Cocos2d -x 声音崩溃

c++ - SFML Audio getSample使用sf::Music实时