繁体   English   中英

android studios游戏结束播放声音出错

[英]Error in playing sound when game is finished in android studios

我试图编写一个代码来玩井字游戏(复仇者主题)......当游戏完成时,将播放相应的声音(3 种不同的声音, .mp3格式和文件大小 < 100kb 组合,持续时间 <3s)。 ..我尝试使用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); ironman = MediaPlayer.create(this, R.raw.i_am); captain = MediaPlayer.create(this, R.raw.do_this); draw = MediaPlayer.create(this, R.raw.giveup);

我在获胜条件下使用 .start() 来播放声音,并在 public void playAgain 中使用.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);

 }
}

这是我的播放声音 function。

//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;
        }
}

将此 function 命名为您要在其中播放音频文件的音频文件的名称。 喜欢:

playSound("do_this");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM