I've built a small app that mutes/unmute the music stream using the class AudioManager
.
In fact, it works until I close the application. i.e. I've muted the sream, I close the app, I restart the app and the button doesn't unmute the stream any more. I've searched in the net but no one seems to have met this problem.
Here's my code:
public class ControlloVolume extends Activity {
ToggleButton tb_mute;
Button btn_mute;
AudioManager mAudioManager;
boolean mute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_controllo_volume);
tb_mute = (ToggleButton) findViewById(R.id.tb_mute);
btn_mute = (Button) findViewById(R.id.btn_mute);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)==0){
mute = true;
tb_mute.setChecked(mute);
}else{
mute = false;
tb_mute.setChecked(mute);
}
}
public void onButtonClicked(View view){
if(!mute){
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
tb_mute.setChecked(true);
mute = true;
}else{
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
tb_mute.setChecked(false);
mute = false;
}
}
First, beware it's not recommended
But I assume you know what you're doing, so here we go.
Note this line from the docs on setStreamMute
I've checked on my device and indeed, when I just exit my activity, stream stays muted. But as soon as I kill the process, mute goes away. Take a look at activity lifecycle.
As your current approach will not work reliably, you could write a foreground service which will trigger the mute - start that service from your activity. Also you would likely need to setStreamSolo.
Two important things.
Volume==0
andmuted
are NOT the same thing. I.e. stream can have volume==0 but be not muted. Though if stream is muted, volume will always be 0As a side note, for such app you would probably want to use widget instead of activity.
Off topic. It seems surprisingly lot of people don't quite get how booleans work. And as I see code such as yours regulary, here is a bit streamlined rewrite.