我在Android中实现了Simon Says的一套Audiogames。游戏工作正常,但我似乎无法使onPause和onStop方法工作:使用我的代码的当前状态,我使用的MediaPlayers不会被释放,如果我添加mediaPlayer.reset()
或mediaPlayer.stop()
应用程序崩溃,在Logcat中给出了这个错误
05-29 19:58:28.471 10205-10205/it.unimi.di.lim.audiogames E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.unimi.di.lim.audiogames, PID: 10205
java.lang.RuntimeException: Unable to pause activity {it.unimi.di.lim.audiogames/it.unimi.di.lim.audiogames.Game2PlayActivity}: java.lang.IllegalStateException
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4592)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4551)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4526)
at android.app.ActivityThread.access$1300(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._reset(Native Method)
at android.media.MediaPlayer.reset(MediaPlayer.java:2052)
at it.unimi.di.lim.audiogames.Game2PlayActivity$3.run(Game2PlayActivity.java:148)
at android.app.Activity.runOnUiThread(Activity.java:6036)
at it.unimi.di.lim.audiogames.Game2PlayActivity.onPause(Game2PlayActivity.java:139)
at android.app.Activity.performPause(Activity.java:7034)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1340)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4578)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4551)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4526)
at android.app.ActivityThread.access$1300(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我想这个错误与我使用Thread同步声音生成这一事实有关。以下是createSequence
函数
private void createSequence(final int i) {
Thread t = new Thread(new Runnable() {
public void run() {
Random rand = new Random();
for (int j = 0; j < i-1; j++) {
final int a = j;
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
playButton(temp.get(a));
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
final int seed = rand.nextInt(4);
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
playButton(seed);
temp.add(seed);
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Button status = (Button) findViewById(R.id.game2_start_button);
status.setText(R.string.game2_repeat);
mWait = false;
}
});
}
});
t.setDaemon(true);
t.start();
}
触发playButton
方法,即播放实际声音的方法
private void playButton(int i) {
Button mc_button = (Button) findViewById(R.id.c_button);
Button md_button = (Button) findViewById(R.id.d_button);
Button me_button = (Button) findViewById(R.id.e_button);
Button mf_button = (Button) findViewById(R.id.f_button);
switch(i) {
case 0:
mc_button.setBackgroundResource(R.color.game2_c_on);
mCMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.c_sound);
mCMediaPlayer.start();
mCMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 1:
md_button.setBackgroundResource(R.color.game2_d_on);
mDMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.d_sound);
mDMediaPlayer.start();
mDMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 2:
me_button.setBackgroundResource(R.color.game2_e_on);
mEMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.e_sound);
mEMediaPlayer.start();
mEMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 3:
mf_button.setBackgroundResource(R.color.game2_f_on);
mFMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.f_sound);
mFMediaPlayer.start();
mFMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
}
}
以下是我OnPause
方法的当前实现(OnStop方法是相同的)。离开活动
@Override
protected void onPause() {
super.onPause();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(mCMediaPlayer != null) {
mCMediaPlayer.release();
mCMediaPlayer = null;
}
if(mDMediaPlayer != null) {
mDMediaPlayer.release();
mDMediaPlayer = null;
}
if(mEMediaPlayer != null) {
mEMediaPlayer.release();
mEMediaPlayer = null;
}
if(mFMediaPlayer != null) {
mFMediaPlayer.release();
mFMediaPlayer = null;
}
}
});
}
提前感谢任何可以指出问题所在的人
如果有任何帮助,请遵循完整的Java和XML代码
爪哇
01001101 01000100, [29.05.17 20:11]
package it.unimi.di.lim.audiogames;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.Random;
public class Game2PlayActivity extends AppCompatActivity {
private int mScore = 0;
private LinkedList<Integer> temp = new LinkedList<);
private boolean mClickable = false;
private boolean mWait = false;
private int mChecking = 0;
private MediaPlayer mCMediaPlayer;
private MediaPlayer mDMediaPlayer;
private MediaPlayer mEMediaPlayer;
private MediaPlayer mFMediaPlayer;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer(mp);
}
};
private void releaseMediaPlayer(MediaPlayer mediaPlayer) {
if (mediaPlayer != null) {
mediaPlayer.release();
Button mc_button = (Button) findViewById(R.id.c_button);
Button md_button = (Button) findViewById(R.id.d_button);
Button me_button = (Button) findViewById(R.id.e_button);
Button mf_button = (Button) findViewById(R.id.f_button);
if(mediaPlayer == mCMediaPlayer) {
mc_button.setBackgroundColor(getResources().getColor(R.color.game2_c_off));
} else if (mediaPlayer == mDMediaPlayer) {
md_button.setBackgroundColor(getResources().getColor(R.color.game2_d_off));
} else if (mediaPlayer == mEMediaPlayer) {
me_button.setBackgroundColor(getResources().getColor(R.color.game2_e_off));
} else {
mf_button.setBackgroundColor(getResources().getColor(R.color.game2_f_off));
}
mediaPlayer = null;
}
}
private void playButton(int i) {
Button mc_button = (Button) findViewById(R.id.c_button);
Button md_button = (Button) findViewById(R.id.d_button);
Button me_button = (Button) findViewById(R.id.e_button);
Button mf_button = (Button) findViewById(R.id.f_button);
switch(i) {
case 0:
mc_button.setBackgroundResource(R.color.game2_c_on);
mCMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.c_sound);
mCMediaPlayer.start();
mCMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 1:
md_button.setBackgroundResource(R.color.game2_d_on);
mDMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.d_sound);
mDMediaPlayer.start();
mDMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 2:
me_button.setBackgroundResource(R.color.game2_e_on);
mEMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.e_sound);
mEMediaPlayer.start();
mEMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
case 3:
mf_button.setBackgroundResource(R.color.game2_f_on);
mFMediaPlayer = MediaPlayer.create(Game2PlayActivity.this, R.raw.f_sound);
mFMediaPlayer.start();
mFMediaPlayer.setOnCompletionListener(mCompletionListener);
break;
}
}
private void createSequence(final int i) {
Thread t = new Thread(new Runnable() {
public void run() {
Random rand = new Random();
for (int j = 0; j < i-1; j++) {
final int a = j;
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
playButton(temp.get(a));
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
final int seed = rand.nextInt(4);
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
playButton(seed);
temp.add(seed);
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
Button status = (Button) findViewById(R.id.game2_start_button);
status.setText(R.string.game2_repeat);
mWait = false;
}
});
}
});
t.setDaemon(true);
t.start();
}
@Override
protected void onPause() {
super.onPause();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(mCMediaPlayer != null) {
mCMediaPlayer.reset();
mCMediaPlayer.release();
mCMediaPlayer = null;
}
if(mDMediaPlayer != null) {
mDMediaPlayer.reset();
mDMediaPlayer.release();
mDMediaPlayer = null;
}
if(mEMediaPlayer != null) {
mEMediaPlayer.reset();
mEMediaPlayer.release();
mEMediaPlayer = null;
}
if(mFMediaPlayer != null) {
mFMediaPlayer.reset();
mFMediaPlayer.release();
mFMediaPlayer = null;
}
}
});
}
@Override
protected void onStop() {
super.onStop();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(mCMediaPlayer != null) {
mCMediaPlayer.reset();
mCMediaPlayer.release();
mCMediaPlayer = null;
}
if(mDMediaPlayer != null) {
mDMediaPlayer.reset();
mDMediaPlayer.release();
mDMediaPlayer = null;
}
if(mEMediaPlayer != null) {
mEMediaPlayer.reset();
mEMediaPlayer.release();
mEMediaPlayer = null;
}
if(mFMediaPlayer != null) {
mFMediaPlayer.reset();
mFMediaPlayer.release();
mFMediaPlayer = null;
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game2_play);
Button mc_button = (Button) findViewById(R.id.c_button);
Button md_button = (Button) findViewById(R.id.d_button);
Button me_button = (Button) findViewById(R.id.e_button);
Button mf_button = (Button) findViewById(R.id.f_button);
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.game2Dark));
window.setNavigationBarColor(this.getResources().getColor(R.color.game2Primary));
}
android.support.v7.app.ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.game2Primary)));
mc_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickable && !mWait) {
playButton(0);
int check = temp.get(mChecking);
mChecking++;
if (check != 0) {
Intent gameOver = new Intent(Game2PlayActivity.this, Game2Activity.class);
gameOver.putExtra("score", mScore);
startActivity(gameOver);
}
if (temp.size() == mChecking) {
mScore++;
TextView score = (TextView) findViewById(R.id.game2_score);
score.setText(Integer.toString(mScore));
mClickable = false;
mChecking = 0;
Button start = (Button) findViewById(R.id.game2_start_button);
start.setText(R.string.game2_start);
}
}
}
});
md_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickable && !mWait) {
playButton(1);
int check = temp.get(mChecking);
mChecking++;
if (check != 1) {
Intent gameOver = new Intent(Game2PlayActivity.this, Game2Activity.class);
gameOver.putExtra("score", mScore);
startActivity(gameOver);
}
if (temp.size() == mChecking) {
mScore++;
TextView score = (TextView) findViewById(R.id.game2_score);
score.setText(Integer.toString(mScore));
mClickable = false;
mChecking = 0;
Button start = (Button) findViewById(R.id.game2_start_button);
start.setText(R.string.game2_start);
}
}
}
});
me_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickable && !mWait) {
playButton(2);
int check = temp.get(mChecking);
mChecking++;
if (check != 2) {
Intent gameOver = new Intent(Game2PlayActivity.this, Game2Activity.class);
gameOver.putExtra("score", mScore);
startActivity(gameOver);
}
if (temp.size() == mChecking) {
mScore++;
TextView score = (TextView) findViewById(R.id.game2_score);
score.setText(Integer.toString(mScore));
mClickable = false;
mChecking = 0;
Button start = (Button) findViewById(R.id.game2_start_button);
start.setText(R.string.game2_start);
}
}
}
});
mf_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mClickable && !mWait) {
playButton(3);
int check = temp.get(mChecking);
mChecking++;
if (check != 3) {
Intent gameOver = new Intent(Game2PlayActivity.this, Game2Activity.class);
gameOver.putExtra("score", mScore);
startActivity(gameOver);
}
if (temp.size() == mChecking) {
mScore++;
TextView score = (TextView) findViewById(R.id.game2_score);
score.setText(Integer.toString(mScore));
mClickable = false;
mChecking = 0;
Button start = (Button) findViewById(R.id.game2_start_button);
start.setText(R.string.game2_start);
}
}
}
});
Button start_button = (Button) findViewById(R.id.game2_start_button);
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!mClickable && !mWait) {
mWait = true;
mClickable = true;
createSequence(mScore+1);
}
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context=".Game1PlayActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/game2_score"
android:textSize="16sp"
android:padding="16dp"
android:gravity="bottom"
android:textAlignment="center" />
<TextView
android:id="@+id/game2_score"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2"
android:text="0"
android:textSize="48sp"
android:padding="16dp"
android:gravity="top"
android:textAlignment="center" />
<Button
android:id="@+id/game2_start_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/game2_start"
android:layout_marginTop="8dp"
android:layout_marginLeft="64dp"
android:layout_marginRight="64dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<Button
android:id="@+id/c_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/game2_c_off"
android:layout_margin="16dp" />
<Button
android:id="@+id/d_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/game2_d_off"
android:layout_margin="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<Button
android:id="@+id/e_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/game2_e_off"
android:layout_margin="16dp" />
<Button
android:id="@+id/f_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/game2_f_off"
android:layout_margin="16dp" />
</LinearLayout>
</LinearLayout>
完整项目(可能有错误):https://www.mediafire.com/?hukauj1waef7va1
答案 0 :(得分:0)
不要打电话
mDMediaPlayer.reset();
之前
mDMediaPlayer.release();
因为方法reset()
将MediaPlayer重置为未初始化状态。
更好地检查onPause()方法中MediaPlayer的状态,因为你可以得到一个&#34; Races&#34;回调mCompletionListener