The audio volume is zero?

2019-01-29 11:09发布

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:

enter image description here

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.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-29 11:22

btn_mute.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if(isChecked) {
                btn_mute.setBackgroundResource(R.drawable.mute_bt);
                audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
            } else {
                btn_mute.setBackgroundResource(R.drawable.volume_bt);
                audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
            }
        }
    });
查看更多
倾城 Initia
3楼-- · 2019-01-29 11:42

I found the issue. I have changed STRAM_MUSIC to STREAM_RING, and the variable userVolumeOnStart.userVolume changes successfully!

查看更多
登录 后发表回答