我编写此代码是为了制作像应用
一样的音板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元素之前添加了if()来检查它们是否为null,我也遵循了这个方法:
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 :我得到了它的工作:),当我重新启动时,我只需要确保 mediaPlayer数组为空应用程序。我还添加了 onPause()和 onResume()函数。
答案 0 :(得分:3)
您在NullPointerException
方法中的第57行获得stopSounds
。你可以自己发布这条线吗?
mediaPlayers
中的一个对象很可能为null。尝试单步执行调试器,看看第一次输入stopSounds
方法时数组的样子。