![](/img/trans.png)
[英]playing a sound by clicking a button with mediaplayer in java in android using 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.