2017-10-09 190 views
1

我對android非常陌生,目前正在開發一個測驗應用程序,其中有用戶使用應用程序時播放的聲音。在我的設置活動中有一個開關按鈕,其中應該打開和關閉應用程序的聲音。 我已經實現了一些邏輯,但它還沒有工作。我已經用盡了我所知道的,我需要一些幫助。關閉整個應用程序的聲音android

style.xml

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:soundEffectsEnabled">true</item> 
    </style> 
    <style name="AppThemeMute" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:soundEffectsEnabled">false</item> 
    </style> 

MyApplication.java

public class MyApplication extends Application { 

    private static int sTheme; 

    public final static int THEME_SOUND_ON = 0; 
    public final static int THEME_SOUND_OFF = 1; 



    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    public static void changeToTheme(Context context, int theme) { 
     sTheme = theme; 

     switch (sTheme) { 
      default: 
      case THEME_SOUND_ON: 
       context.setTheme(R.style.AppTheme); 
       break; 
      case THEME_SOUND_OFF: 
       context.setTheme(R.style.AppThemeMute); 
       break; 
     } 
    } 
} 

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity { 
    private Switch mSoundSwitch; 
    private Switch mAdsSwitch; 
    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
     // handling switch button 
     mSoundSwitch = (Switch) findViewById(R.id.soundSwitch); 
     mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
if (isChecked) { 
    sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    editor = sharedPreferences.edit(); 
    editor.putBoolean("sound", true); 
    editor.apply(); 
    // Change Whole App Theme 
    MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_ON); 
} else { 
    sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    editor = sharedPreferences.edit(); 
    editor.putBoolean("sound", false); 
    editor.apply(); 
    MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_OFF); 
} 
      } 
     }); 

     sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
     boolean isChecked = sharedPreferences.getBoolean("sound", true); 
     mSoundSwitch.setChecked(isChecked); 

    } 

} 

現在的S女巫按鈕完美搭配sharedPreferences。但是,按鈕切換時聲音不會被靜音。

+0

我認爲你沒有重新創建你目前的活動可能有效。在你的'changeToTheme'方法結束之前使用'recreate()' – UltimateDevil

+0

或者如果這不適合你,那麼檢查[here](https://stackoverflow.com/questions/14031399/issue-with-unmute-button-java -android) – UltimateDevil

+1

查看https://stackoverflow.com/questions/10895882/mute-the-global-sound-in-android – Robert

回答

0
private void mute() { 
    //mute audio 
    AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true); 
} 

public void unmute() { 
    //unmute audio 
    AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false); 
} 

這種雙功能可以處理音頻muteunmute相當不錯。

根據用戶preference,您可以簡單地將切換/切換爲muteunmute

+0

我如何創建全局變量? –

+0

好吧,我已經很容易檢查我編輯的答案 –

+0

這將使所有應用程序的通知流靜音。 –