【问题标题】:Error in playing sound when game is finished in android studios在android studio中完成游戏时播放声音错误
【发布时间】:2021-06-02 21:01:56
【问题描述】:

我尝试编写代码来玩井字游戏(复仇者主题)...当游戏结束时,将播放相应的声音(.mp3 格式的 3 种不同声音和文件大小 MediaPlayer 进行此操作,当我们连续玩游戏时它最多只能工作 2 回合...之后,它不会播放相应的声音游戏结束了,即使我在几回合后继续玩,它会同时播放所有三种声音,并且应用程序崩溃。

注意:游戏运行良好,唯一的问题是声音。

初始化: private MediaPlayer ironman, captain, draw;

public void click(View view)中的mp3文件调用为ironman = MediaPlayer.create(this, R.raw.i_am); captain = MediaPlayer.create(this, R.raw.do_this); draw = MediaPlayer.create(this, R.raw.giveup);

我在获胜条件下使用.start() 播放声音并使用.pause() 停止public void playAgain (View view) 中的声音

MainActivity.java 完整代码:

public class MainActivity<string> extends AppCompatActivity {

//to track the boxes (if value is 2 it is taken as empty box)
int[] position = {2, 2, 2, 2, 2, 2, 2, 2, 2};

//used to decide who's turn to play
int active;

//helps to stop the after a player is the winner
boolean activeGame = true;

//to display the winner text
String winner = "";

private MediaPlayer ironman, captain, draw;


public void click(View view) {

    ironman = MediaPlayer.create(this, R.raw.i_am);
    captain = MediaPlayer.create(this, R.raw.do_this);
    draw = MediaPlayer.create(this, R.raw.giveup);

    ImageView iv = (ImageView) view;
    TextView turn = (TextView) findViewById(R.id.turn);

    int tagPosition = Integer.parseInt(iv.getTag().toString());

  //checks valid move or not and whether game is active or not
  if(position[tagPosition] == 2 && activeGame) {

      //animation
      iv.animate().alpha(1).setDuration(300);

      // 0 : captain america, 1 : ironman , 2 : empty
      //also flips the active value (change the current player)
      if (active == 0) {
          iv.setImageResource(R.drawable.captainamerica);
          position[tagPosition] = active;
          active =1;
          turn.setText("Ironman's turn.");
      }else {
          iv.setImageResource(R.drawable.ironman);
          position[tagPosition] = active;
          active =0;
          turn.setText("Captain america's turn.");
      }

      //checking whether game is over or not
      //win condition for Captain america
      if((position[0]==0 && position[1]==0 && position[2]==0) || (position[3]==0 && position[4]==0 && position[5]==0) || (position[6]==0 && position[7]==0 && position[8]==0) ||
              (position[0]==0 && position[3]==0 && position[6]==0) || (position[1]==0 && position[4]==0 && position[7]==0) || (position[2]==0 && position[5]==0 && position[8]==0) ||
              (position[0]==0 && position[4]==0 && position[8]==0) || (position[2]==0 && position[4]==0 && position[6]==0)){

          captain.start();
          turn.setVisibility(View.INVISIBLE);
          activeGame = false;
          winner = "Captain america won the game!!";
          gameFinish(winner);

      }
      //win condition for Ironman
      else if((position[0]==1 && position[1]==1 && position[2]==1) || (position[3]==1 && position[4]==1 && position[5]==1) || (position[6]==1 && position[7]==1 && position[8]==1) ||
              (position[0]==1 && position[3]==1 && position[6]==1) || (position[1]==1 && position[4]==1 && position[7]==1) || (position[2]==1 && position[5]==1 && position[8]==1) ||
              (position[0]==1 && position[4]==1 && position[8]==1) || (position[2]==1 && position[4]==1 && position[6]==1)){

          ironman.start();
          turn.setVisibility(View.INVISIBLE);
          activeGame = false;
          winner = "Ironman won the game!!";
          gameFinish(winner);

      }
      //condition for draw match
      else if(isTied() ){

          draw.start();
          turn.setVisibility(View.INVISIBLE);
          winner = "Game is Draw, try again!!";
          gameFinish(winner);

      }
  }
}



//logic for playAgain button
public void playAgain (View view) {
   TextView tvResult = (TextView) findViewById(R.id.tvResult);

   Button btnPlayAgain = (Button) findViewById(R.id.btnPlayAgain);

   TextView name = (TextView) findViewById(R.id.name);

   TextView turn = (TextView) findViewById(R.id.turn);

   tvResult.setVisibility(View.INVISIBLE);
   btnPlayAgain.setVisibility(View.INVISIBLE);
   name.setVisibility(View.INVISIBLE);
   turn.setVisibility(View.VISIBLE);

   turn.setText("Select a grid to start the game.\nIt's Captain america's turn.");

   GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);

   draw.pause();ironman.pause();captain.pause();

   for (int i = 0; i < gridLayout.getChildCount(); i++) {

       ImageView imageView = (ImageView) gridLayout.getChildAt(i);
       imageView.animate().alpha(0).setDuration(300);
       imageView.setImageDrawable(null);
   }

   for (int i = 0; i < position.length; i++) {
       position[i] = 2;
   }
   active = 0;
   activeGame = true;
}


//to print the winner and for displaying the text and playAgain btn
public void gameFinish(String winner){

   TextView tvResult = (TextView) findViewById(R.id.tvResult);

   Button btnPlayAgain = (Button) findViewById(R.id.btnPlayAgain);

   TextView name = (TextView) findViewById(R.id.name);

   tvResult.setVisibility(View.VISIBLE);
   btnPlayAgain.setVisibility(View.VISIBLE);
   name.setVisibility(View.VISIBLE);

   tvResult.setText(winner);
}

//checks every value in position if there is 2 it indicates there are empty boxes
public boolean isTied(){
    
    for(int i=0; i<position.length; i++){
        if(position[i]==2){
            return false;
        }
    }
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 }
}

【问题讨论】:

    标签: java android-studio android-mediaplayer tic-tac-toe


    【解决方案1】:

    这是我的播放声音功能。

    //Your code..
    private MediaPlayer soundPlayer;
    private static final String TAG = "MUSIC ERROR";
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        soundPlayer = MediaPlayer.create(this.context, R.raw.giveup);
    }
    //Your code 
    
    
       
    private void playSound(String soundID) {
        try {
                if (soundPlayer != null && soundPlayer.isPlaying()) {
                    soundPlayer.pause();
                }
                soundPlayer = MediaPlayer.create(this,
                        getResources().getIdentifier(soundID, "raw", this.getPackageName())
                );
                soundPlayer.setVolume(100, 100);
                soundPlayer.setLooping(false);
                soundPlayer.start();
                soundPlayer.setOnCompletionListener(mp -> {
                    mp.release();
                    soundPlayer = null;
                });
    
            }catch (Exception e) {
                Log.d(TAG, "playSound: SOMETHING WENT WRONG");
                soundPlayer.release();
                soundPlayer = null;
            }
    }
    

    使用要在其中播放音频文件的音频文件的名称调用此函数。喜欢:

    playSound("do_this");
    

    【讨论】:

      猜你喜欢
      • 2011-08-01
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多