我是Android上的新开发者.我有一个带有Togglebutton的视图,当按下它时会关闭所有声音,直到再次按下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上的所有声音所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)