I have an application with a mute button. I have been stuck on this for 4 and a half hours now, so I am plain desperate. I am trying to have the volume mute when user clicks mute and unmute when user clicks it again. This works fine, except the audio isn't playing. For some reason, Stream_Music is 0?
public void mute(View view) {
mutebutton = (ImageButton) findViewById(R.id.mutebutton);
if ((variableForMute.x % 2) != 0) { //If it's odd UNMUTE
Log.v(TAG, "Use this volume to unmute " +userVolumeOnStart.userVolume +"");
Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show();
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, userVolumeOnStart.userVolume, 0);
variableForMute.x++;
} else { //If its even MUTE
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.v(TAG, "Right Before Mute " +userVolumeOnStart.userVolume +"");
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
variableForMute.x++;
}
}
Here is the beginning where I declare the variables and objects:
ImageButton leftcenter;
ImageButton mutebutton;
final String TAG = "userVolume";
private AudioManager mAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
leftcenter = (ImageButton) findViewById(R.id.startGame);
}
The thing is, as log outputs, I am getting the following:
I should have the volume change to what my volume was before I started my app. I want to be able to have the user unmute the app to the same volume that the user was originally on. But for some reason it is always 0!? Why is this happening? My phone volume is at full?!
Please help,
I have spent way too much time on this-I appreciate all feedback: positive, negative, neutral, whatever.
btn_mute.setOnCheckedChangeListener(new OnCheckedChangeListener() {
I found the issue. I have changed
STRAM_MUSIC
toSTREAM_RING
, and the variableuserVolumeOnStart.userVolume
changes successfully!