The audio volume is zero?

允我心安 提交于 2019-12-29 09:37: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:

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.


回答1:


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




回答2:


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);
            }
        }
    });


来源:https://stackoverflow.com/questions/33589116/the-audio-volume-is-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!