如何静音在android系统? [英] How to mute sound in android?

查看:265
本文介绍了如何静音在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能解决这个问题?我觉得我要救我的静音功能也更不知道如何做到这一点。

How can I solve this Problem? I think I have to save my mute function also but don't know how to do this.

我要问这个我怎么能在我的活动根据活动设置的复选框,preference静音。
我的设置页面看起来像这样。

I want to ask that how I can mute sound in my activity according to the check box preference in Settings activity. My settings page look like this.

public class Settings extends PreferenceActivity  {

CheckBoxPreference soundPref;
AudioManager mAudioManager = null;

final String SOUND = "soundPreference";

 protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
    setPreferenceScreen(createPreferenceHierarchy());
}

     private PreferenceScreen createPreferenceHierarchy() {
    // Root
    PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

    // Dialog based preferences
    PreferenceCategory category1 = new PreferenceCategory(this);

    root.addPreference( category1 );

    soundPref = new CheckBoxPreference( this );

    soundPref.setTitle(R.string.sound);
    soundPref.setKey(SOUND);

    /**
     * Adding all items to Preferences
     */
    category1.addPreference(soundPref);

    soundPref.setOnPreferenceClickListener(new ReflectiveAction( this, "evSound" ) );

    return root;
}

   public void evSound()
  {
    if(soundPref.isChecked())
        {Toast.makeText(getBaseContext(),
                R.string.sound_enable,
                Toast.LENGTH_SHORT).show();
            setSoundText();
            if(mAudioManager!=null) {
            mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
            }
        }
        else
        {
            Toast.makeText(getBaseContext(),
                    R.string.sound_disable,
                    Toast.LENGTH_SHORT).show();
            setSoundText();
              if(mAudioManager!=null) {
              mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);
              }
        }

和我在我的MediaPlayer通过例如另一活动产生的声音。 mp.start()。

and I am producing sounds in my another activity by MediaPlayer instance. mp.start().

问题是,每当我第一次运行我的应用程序需要访问设置,然后单击复选框以选择并再次取消选择,然后声音静音。但是当我运行应用程序,先去我的活动中,我产生一个声音,声音不静音。我在第一次访问设置页面禁用声音。

the problem is that whenever i run my application first i have to visit settings and click on checkbox to select and again for unselect then sound is mute. but when I run application and first go to my activity in which I produces a sound, sound is not mute. I have to visit settings page at first to disable sound.

根据上述编码我复选框状态做的是保存在我的preference关键,但我的应用程序不节能静音功能,这就是为什么我要访问设置页面第一次使静音功能激活。

By doing above coding my checkBox state is stored in my Preference key but my application is not saving mute sound function that's why i have to visit Settings page first to make mute function active.

我怎样才能解决这个问题?我觉得我要救我的静音功能也更不知道如何做到这一点。

How can I solve this Problem? I think I have to save my mute function also but don't know how to do this.

推荐答案

我假设你正在使用的preferenceActivity类的一个实例。

I am assuming you're using an instance of the PreferenceActivity class.

在你的活动,你需要设置的静音状态。

In your activity, you need to set the mute state.

if(PreferenceManager.getSharedPreferences(this).getBoolean("soundprefkey", false))
{
    /* mute */
} else {
    /* unmute */
}

在哪里的声音prefkey是你在preferences XML定义的关键。

Where "soundprefkey" is the key as defined in your preferences xml.

如果你不使用preferenceActivity类,请考虑这样做。你可以看看这个

If you're not using the PreferenceActivity class, please consider doing this. You may take a look at this.

这篇关于如何静音在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆