通过 setVolume(0,0) 静音时保持 .isPlaying() 为 True

Keeping .isPlaying() on True while muting sound via setVolume(0,0)

我在我的应用程序上使用音频静音功能时遇到了意外问题。我在 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();
        }
    }

非常感谢您提供的任何帮助!

我现在已经通过将“.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 的状态不变,同时按预期静音。