gpt4 book ai didi

android - 如何关闭 Android 上的所有声音

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:49 25 4
gpt4 key购买 nike

我是 Android 的新开发者。我有一个带有 ToggleButton 的 View ,按下时将关闭所有声音,直到再次按下 ToggleButton。

目前,我有这段代码。我可以看到正在发生的日志记录事件,但声音没有关闭。谁能提供一些见解?

 public void onToggleClicked(View view) {
Log.i("onToggleClicked", "ToggleClick Event Started");
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();

if (on) {
Log.i("onToggleIsChecked", "ToggleClick Is On");
//turn off sound, disable notifications
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.i("STREAM_SYSTEM", "Set to true");
//notifications
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
Log.i("STREAM_NOTIFICATION", "Set to true");
//alarm
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
Log.i("STREAM_ALARM", "Set to true");
//ringer
amanager.setStreamMute(AudioManager.STREAM_RING, true);
Log.i("STREAM_RING", "Set to true");
//media
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
Log.i("STREAM_MUSIC", "Set to true");

} else {
Log.i("onToggleIsChecked", "ToggleClick Is Off");
// turn on sound, enable notifications
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Log.i("STREAM_SYSTEM", "Set to False");
//notifications
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
Log.i("STREAM_NOTIFICATION", "Set to False");
//alarm
amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
Log.i("STREAM_ALARM", "Set to False");
//ringer
amanager.setStreamMute(AudioManager.STREAM_RING, false);
Log.i("STREAM_RING", "Set to False");
//media
amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
Log.i("STREAM_MUSIC", "Set to False");
}
Log.i("onToggleClicked", "ToggleClick Event Ended");
}

提前致谢!

最佳答案

我能够通过将事件更改为监听器来使其工作

    public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//create the toggle button ref and instantiate
ToggleButton tb = (ToggleButton)this.findViewById(R.id.tglSetStatus);
tb.setTextOff(getString(R.string.available_status));
tb.setTextOn(getString(R.string.driving_status));
//default to being available
tb.setChecked(false);
// attach an OnClickListener
tb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// your click actions go here
Log.i("onToggleClicked", "ToggleClick Event Started");
//an AudioManager object, to change the volume settings
AudioManager amanager;
amanager = (AudioManager)getSystemService(AUDIO_SERVICE);

// Is the toggle on?
boolean on = ((ToggleButton)v).isChecked();

if (on) {
Log.i("onToggleIsChecked", "ToggleClick Is On");
//turn ringer silent
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Log.i("RINGER_MODE_SILENT", "Set to true");

//turn off sound, disable notifications
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
Log.i("STREAM_SYSTEM", "Set to true");
//notifications
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
Log.i("STREAM_NOTIFICATION", "Set to true");
//alarm
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
Log.i("STREAM_ALARM", "Set to true");
//ringer
amanager.setStreamMute(AudioManager.STREAM_RING, true);
Log.i("STREAM_RING", "Set to true");
//media
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
Log.i("STREAM_MUSIC", "Set to true");

} else {
Log.i("onToggleIsChecked", "ToggleClick Is Off");

//turn ringer silent
amanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Log.i(".RINGER_MODE_NORMAL", "Set to true");

// turn on sound, enable notifications
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Log.i("STREAM_SYSTEM", "Set to False");
//notifications
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
Log.i("STREAM_NOTIFICATION", "Set to False");
//alarm
amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
Log.i("STREAM_ALARM", "Set to False");
//ringer
amanager.setStreamMute(AudioManager.STREAM_RING, false);
Log.i("STREAM_RING", "Set to False");
//media
amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
Log.i("STREAM_MUSIC", "Set to False");
}
Log.i("onToggleClicked", "ToggleClick Event Ended");
}
});

我通过查看这些问题找到了输入:How to turn off all sounds and vibration via Android API还有这个:attach onClickListener to ToggleButton

关于android - 如何关闭 Android 上的所有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12353629/

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