问题
My audio isn't playing...I've been stuck on this for literally 6 hours, and have no clue what the problem is. I am trying to have the volume mute when user clicks mute and unmute when user clicks it again.
I want to be able to have the user unmute the app to the same volume that the user was originally on. Their original volume is stored in userVolumeOnStart.userVolume
, I use this value in the unMute, to set their volume back.
Here is the listener to the mute button:
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_RING, userVolumeOnStart.userVolume, 0);
variableForMute.x++;
} else { //If its even MUTE
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
Log.v(TAG, "Right Before Mute " +userVolumeOnStart.userVolume +"");
mAudioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
variableForMute.x++;
}
}
userVolumeOnStart.userVolume is a variable that I defined in a seperate class, to use it globally. Here is where the audio actually runs. I can assure you the issue is not here, but I will still provide the code anyway:
cdt = new CountDownTimer(20000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.v(TAG, millisUntilFinished + "left");
millisUntilFinishedRounded = ((500 + millisUntilFinished) / 1000) * 1000;
if (millisUntilFinishedRounded == 10000) { //TEN SECONDS
ten = MediaPlayer.create(TwentySeconds.this, R.raw.tenmoreseconds);
ten.start();
}
if (millisUntilFinishedRounded == 3000) {
three = MediaPlayer.create(TwentySeconds.this, R.raw.threetwoone);
three.start();
}
}
This is a service that runs in the background when the app starts.
Here is my log after changing ringer volume on my phone while app is running and pressing the mute button. It shows that userVolumeOnStart.userVolume is doing what it's supposed to be doing:
I am desperate now, so I would really appreciate any feedback (negative or positive!)
Thanks so much,
Rich
By the way, my question is not a duplicate of The audio volume is zero?. I have changed the audio stream since that question, and have figured out the problem there. There, there was an issue with AudioManager.STREAM_MUSIC
where userVolumeOnStart.userVolume
would always be 0. That is not the issue now, as userVolumeOnStart.userVolume
successfully changes based on user volume. The problem now is that the Media Players are not effectively workung for an unknown reason.
来源:https://stackoverflow.com/questions/33589665/audio-not-playing