在我的应用程序上处理音频静音功能时,我遇到了意想不到的问题。我对isPlaying()==true的状态设置了多个条件,但是当玩家静音应用程序(mysound.setVolume(0,0) )时,我的应用程序充当isPlaying()==False,因此我的所有if条件都停止工作。对于.setVolume(0,0),我希望我的MediaPlayer仍然在后台运行,只有没有声音的->仍然触发isPlaying()==True上的条件。
你们能给我一些建议吗?我尝试将卷设置为最小值(0.001f,0.001f)来欺骗它,但它再次充当了isPlaying()==False。
我也尝试了(0.01f,0.01f),这导致了全音量的声音,这也是不必要的。
下面是链接到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;
}
}单击静音示例代码:
@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;
}
}声音初始化示例代码:
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();
}
}非常感谢您提供的任何帮助!
发布于 2020-06-23 19:42:00
我现在通过将".setVolume(0,0)“逻辑替换为:
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);这使.isPlaying()==True的状态保持不变,同时按预期静音。
https://stackoverflow.com/questions/62501177
复制相似问题