I encountered unexpected issue while working with audio mute functions on my app. I have multiple conditions set on status of isPlaying()==true but when players mute the app (mysound.setVolume(0,0), my app acts as isPlaying()==False, thus all of my if conditions stop working. With .setVolume(0,0) I expected my MediaPlayer to be still running in the background only with no sound -> still triggering my conditions on isPlaying()==True.
Do you guys please have some advise how I could implement this? I tried setting volume to its bare minimum (0.001f, 0.001f) to trick this but it acted as isPlaying()==False again.
I also tried (0.01f, 0.01f), this resulted in full volume sound which was also unwanted.
Here is sample of my condition linked to isPlaying()==True :
@Override
public void update() {
if (Score.getScore() <= 7 ) {
xPosition-=speed+0.3;
separation= separation-0.15;
}else if(GameManager.getMpMotivationBuildup1().isPlaying()==true){
xPosition-=speed+0.35;
separation= separation-0.40;
}
}
Sample code for mute on click:
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.image_button_android:
if(getGameState()==GameState.GAME_OVER) {
GameManager.gameState=GameState.INITIAL;
Random rand=new Random();
int[] images={
R.drawable.motivation1,R.drawable.motivation2,R.drawable.motivation3
,R.drawable.motivation4,R.drawable.motivation5,R.drawable.motivation6
,R.drawable.motivation7,R.drawable.motivation8,R.drawable.motivation9
,R.drawable.motivation10,R.drawable.motivation11,R.drawable.motivation12
,R.drawable.motivation13,R.drawable.motivation14,R.drawable.motivation15
,R.drawable.motivation16,R.drawable.motivation17,R.drawable.motivation18
,R.drawable.motivation19,R.drawable.motivation20,R.drawable.motivation21
,R.drawable.motivation22,R.drawable.motivation23,R.drawable.motivation24
,R.drawable.motivation25,R.drawable.motivation26,R.drawable.motivation27
,R.drawable.motivation28,R.drawable.motivation29,R.drawable.motivation30
};
MainActivity.motivateme.setImageResource(images[rand.nextInt(images.length)]);
}else {
}
break;
case R.id.rate_button:
MainActivity.getInstance().rateUs((android.view.View) View);
break;
case R.id.volume_on:
//MainActivity.vibe.vibrate(80);
MainActivity.VolumeOn.setVisibility(VISIBLE);
MainActivity.VolumeOff.setVisibility(INVISIBLE);
paused=true;
//Muting sound
GameManager.getMpPoint().setVolume(0,0);
GameManager.getMpSwoosh().setVolume(0,0);
GameManager.getMpDie().setVolume(0,0);
GameManager.getMpHit().setVolume(0,0);
GameManager.getMpWing().setVolume(0f,0f);
GameManager.getMpMetal().setVolume(0f,0f);
GameManager.getMpCutePiano().setVolume(0,0f);
GameManager.getMpWildWest().setVolume(0f,0f);
GameManager.getMpMotivationBuildup1().setVolume(0f,0f);
GameManager.getMpMotivationBuildup2().setVolume(0f,0f);
GameManager.getMpMotivationBuildup3().setVolume(0f,0f);
break;
case R.id.volume_off:
MainActivity.VolumeOn.setVisibility(INVISIBLE);
MainActivity.VolumeOff.setVisibility(VISIBLE);
paused=false;
//Enabling sound
GameManager.getMpPoint().setVolume(1,1);
GameManager.getMpSwoosh().setVolume(1,1);
GameManager.getMpDie().setVolume(1,1);
GameManager.getMpHit().setVolume(1,1);
GameManager.getMpWing().setVolume(1,1);
GameManager.getMpWing().setVolume(1,1);
GameManager.getMpMetal().setVolume(1,1);
GameManager.getMpCutePiano().setVolume(1,1);
GameManager.getMpWildWest().setVolume(1,1);
GameManager.getMpMotivationBuildup1().setVolume(1,1);
GameManager.getMpMotivationBuildup2().setVolume(1,1);
GameManager.getMpMotivationBuildup3().setVolume(1,1);
break;
default:
break;
}
}
Sample code of sounds initialization:
public void update() {
switch (gameState) {
case PLAYING:
bird.update();
obstacleManager.update(); //updating of pipes not needed in gameover
if(Score.getScore()==7){
mpMotivationBuildup1.start();
}
if((Score.getScore()==25) && (mpMotivationBuildup1.isPlaying()==false)){
// mpMetal.reset();
//mpMetal.prepare();
mpWildWest.start();
}
if((Score.getScore()==40) && mpWildWest.isPlaying()==false){
mpMotivationBuildup2.start();
}
if((Score.getScore()==60) && (mpMotivationBuildup2.isPlaying()==false)){
mpMetal.start();
}
if((Score.getScore()==80) && mpMetal.isPlaying()==false){
mpCutePiano.start();
}
if((Score.getScore()==100)&& mpMetal.isPlaying()==false){
mpMotivationBuildup3.start();
}
if((Score.getScore()==130) && mpMotivationBuildup2.isPlaying()==false){
mpMetal.start();
}
updateButtons();
break;
case GAME_OVER:
bird.update();
mpCutePiano.stop();
mpWildWest.stop();
mpMetal.stop();//stops metal on game over
mpMotivationBuildup1.stop();
mpMotivationBuildup2.stop();
mpMotivationBuildup3.stop();
updateButtons();
break;
case INITIAL:
initGame();
if(GameOverButtonsTouch.isPaused()==false) {
mpMetal=MediaPlayer.create(getContext(),R.raw.metal);
mpCutePiano=MediaPlayer.create(getContext(),R.raw.cutepiano);
mpWildWest=MediaPlayer.create(getContext(),R.raw.wildwest);
mpMotivationBuildup1=MediaPlayer.create(getContext(),R.raw.mbuildp1);
mpMotivationBuildup2=MediaPlayer.create(getContext(),R.raw.mbuildp2);
mpMotivationBuildup3=MediaPlayer.create(getContext(),R.raw.mbuildp3);
}else {
}
updateButtons();
}
}
Thank you very much for any help provided!
I have now solved the issue by replacing ".setVolume(0,0)" logic with:
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int current_volume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int set_volume=0;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,set_volume, 0);
This keeps the state of .isPlaying()==True unchanged while muting the volume as expected.