gpt4 book ai didi

java - 为Android取消静音

转载 作者:行者123 更新时间:2023-12-03 01:41:36 26 4
gpt4 key购买 nike

我有一个应用程序,其中我实现了一个切换按钮来打开和关闭音频。我非常了解,使用adjustStreamVolume仅适用于api 23及更高版本,而setStreamMute已弃用,因此我由逻辑进行了适当处理。
但是,我在设备api低于api 23的音频状态方面遇到问题,因为在之前被静音后它不会取消静音。无论如何,对于api 23及更高版本,开关和音频状态都可以正常工作,但是我需要有关如何针对低于23的设备api正确静音和取消静音设备的帮助。
这是我的逻辑。

public class SettingsActivity extends AppCompatActivity {
private Switch mSoundSwitch;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
AudioManager aManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
mSoundSwitch = (Switch) findViewById(R.id.soundSwitch);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
unmute();
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", true);
editor.apply();

} else {
mute();
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("sound", false);
editor.apply();

}
}
});
// retrieving value from shared preferences (sound)
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
boolean isChecked = sharedPreferences.getBoolean("sound", true);
mSoundSwitch.setChecked(isChecked);

}
private void mute() {
//mute audio
aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
}

public void unmute() {
//unmute audio
aManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE, 0);
} else {
aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}

}

最佳答案

我用以下代码解决了我的问题:

 private void mute() {
//mute audio
aManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
} else {
aManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
}

public void unmute() {
//unmute audio
aManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE, 0);
} else {
aManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
}
}

关于java - 为Android取消静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46980650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com