我为了使音板像应用应用程序崩溃,当按钮被点击
public class ButtonSoundActivity extends Activity implements OnClickListener {
private Button buttons[];
private MediaPlayer mediaPlayers[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onPause() {
super.onPause();
releaseSounds();
}
@Override
protected void onResume() {
super.onResume();
initContent();
}
@Override
protected void onStop() {
super.onStop();
releaseSounds();
}
/**
* Initialise all the content :
* buttons, media players and set click listeners for the buttons
*/
private void initContent() {
buttons = new Button[5];
mediaPlayers = new MediaPlayer[5];
buttons[0] = (Button)findViewById(R.id.button0);
buttons[1] = (Button)findViewById(R.id.button1);
buttons[2] = (Button)findViewById(R.id.button2);
buttons[3] = (Button)findViewById(R.id.button3);
buttons[4] = (Button)findViewById(R.id.button4);
mediaPlayers[0] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound1);
mediaPlayers[1] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound2);
mediaPlayers[2] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound3);
mediaPlayers[3] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound4);
mediaPlayers[4] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound5);
for(int i=0; i<5; i++) {
buttons[i].setOnClickListener(this);
}
}
public void onClick(View v) {
stopSounds();
for(int i=0; i<5; i++)
if(v.getId() == buttons[i].getId()) {
if(mediaPlayers[i] != null)
mediaPlayers[i].start();
}
}
/**
* Stop the previous sound when another button is clicked
* so that the sounds don't overlap
*/
private void stopSounds() {
for(int i=0; i<5 ;i++)
if(mediaPlayers[i] != null)
if(mediaPlayers[i].isPlaying()) {
mediaPlayers[i].pause();
mediaPlayers[i].seekTo(0);
}
}
/**
* Release all sounds and empty the mediaPlayers array
*/
private void releaseSounds() {
for(int i=0; i<5;i++) {
if(mediaPlayers[i] != null) {
mediaPlayers[i].stop();
mediaPlayers[i].release();
mediaPlayers[i] = null;
}
}
}
}
XML文件写了这个代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="button0" android:id="@+id/button0"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="button1" android:id="@+id/button1"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="button2" android:id="@+id/button2"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="button3" android:id="@+id/button3"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="button4" android:id="@+id/button4"/>
</LinearLayout>
的问题是,如果我点击一个按钮多声音不再播放,或者某个时候,当我第一次打开应用程序时,它会在我点击一个按钮时崩溃。 错误说,这是关系到stopSounds方法
因此,没有人有答案吗?
编辑:我加入了,如果()之前每个MediaPlayer的元素来检查自己是否为空,还我跟着这个方法:
AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.sound1);
mediaPlayers[0] = new MediaPlayer();
try {
mediaPlayers[0].setDataSource(fileDescriptor.getFileDescriptor());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayers[0].prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
,但没有任何运气:(
编辑2:我得到了它的工作:),我必须确保该媒体播放器阵列是空的,当我重启应用。我还添加了的onPause()和的onResume()功能。
你的代码的第57行有一个NullPointerException,这行是什么? – Egor