【问题标题】:how to set sound on and off programmatically in android如何在android中以编程方式打开和关闭声音
【发布时间】:2011-06-14 09:43:49
【问题描述】:

在我的 JumbledWords 游戏应用程序中,我提供了打开和关闭声音的选项。问题是我无法做到这一点。我已经为它编写了代码,但它不起作用。

SplashScreen.java

RadioButton rbSoundOn, rbSoundOff;
JumbledWords jw = new JumbledWords();
@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);

    //set the full screen view of the activity
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash_screen);

    rbSoundOn =  (RadioButton)findViewById(R.id.optSoundOn);
    rbSoundOff =  (RadioButton)findViewById(R.id.optSoundOff);

    if(rbSoundOn.isChecked() == true)
    {
        jw.setSoundOn(true);
    }
    else
    {
        jw.setSoundOn(false);
    }}

JumbledWords.java

static boolean soundOn;
public void setSoundOn(boolean soundOn)
{
    this.soundOn = soundOn;
}

public boolean isSoundOn()
{
    return soundOn; 
}

public void checkWord()
{
    if(abcd.equalsIgnoreCase(etGuessedWord.getText().toString()))
    {
        WordLibrary.setMyInt(WordLibrary.getMyInt() + 10);
        tvScore.setText(String.valueOf(WordLibrary.getMyInt()));
        if(soundOn == true)
        {
         mp = MediaPlayer.create(this, R.raw.clap);
         mp.start();

         mp.setOnCompletionListener(new OnCompletionListener(){

            @Override
            public void onCompletion(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                mp.release();
            }

         });
        }
    }
    else
    {
        if(soundOn == true)
        {
         mp = MediaPlayer.create(this, R.raw.oop);
         mp.start();

         mp.setOnCompletionListener(new OnCompletionListener(){

                @Override
                public void onCompletion(MediaPlayer arg0) {
                    // TODO Auto-generated method stub
                    mp.release();
                }

             });
        }


    }
}

我的问题是,如果我使用关闭选项,我的声音会播放,这在这种情况下一定不会发生。请帮帮我。

【问题讨论】:

  • 一个潜在的问题可能是您应该致电mp.setOnCompletionListener() 之前 mp.onStart()。您可能没有正确释放 MediaPlayer 实例。不过,我不确定这是否真的导致了您的问题。

标签: android audio


【解决方案1】:

您实际上并未处理对单选按钮的更改

您需要定义包装 RadioButton 控件的 RadioGroup 控件,并定义单选按钮的“组”;然后在 RadioGroup 上调用 setOnCheckedChangeListener() 来定义一个监听器,当状态改变时它会被调用。您需要在此侦听器中检查各个按钮的状态并调用jw.setSoundOn();,而不是在您的onCreate() 方法中进行:

public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);

    //set the full screen view of the activity
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash_screen);
    RadioGroup group = (RadioGroup)findViewById( R.id.optSoundGroup );
    final RadioButton rbSoundOn =  (RadioButton)findViewById(R.id.optSoundOn);
    final RadioButton rbSoundOff =  (RadioButton)findViewById(R.id.optSoundOff);
    group.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        void onCheckedChanged(RadioGroup group, int checkedId)
        {
            if(rbSoundOn.isChecked() == true)
            {
                jw.setSoundOn(true);
            }
            else
            {
                jw.setSoundOn(false);
            }
        }
    } );
}

【讨论】:

    【解决方案2】:

    不确定问题出在哪里,但在您的代码中发现了一些问题: 1)初始化对象时不要创建对象:

    JumbledWords jw = new JumbledWords();
    

    不对。您应该初始化变量,然后在活动的 onCreate 中调用构造函数:

    JumbledWords jw;
    

    onCreate()里面:

    jw = new JumbledWords();
    

    2) 如果soundOn 被声明为静态,则使用此变量的每个方法也应该是静态的。您应该以静态方式调用这些方法:

    JumbledWords.setSoundOn(true);
    

    尝试解决这些问题,也许您会解决您的问题。祝你好运!

    【讨论】:

      【解决方案3】:

      查看我在这篇文章中编写的代码。您可以了解如何使用媒体和铃声音量并向用户展示您正在更改它。

      How do you get/set media volume (not ringtone volume) in Android?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2015-05-27
        相关资源
        最近更新 更多