我正在尝试制作一个简单的音乐播放器应用程序,其中包含ImageButton
用于播放和暂停的简单 s。
我试过这样做,但在运行时从模拟器中出现错误“不幸的是无法打开应用程序”。
我只尝试了播放按钮的代码并且它工作但是当添加暂停按钮的代码时会发生运行时错误。谁能告诉我哪里出错了?
谢谢你。
public class Audio1Activity extends Activity {
private static final String TAG = "AudioDemo";
private static final String isPlaying = "Media is Playing";
private static final String notPlaying = "Media has stopped Playing";
private MediaPlayer player;
ImageButton playbtn,pausebtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
player= MediaPlayer.create(this , R.raw.a1);
playbtn=(ImageButton) this.findViewById(R.id.image_button1);
player.setLooping(false);
playbtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
demoPlay();
}
});
pausebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
demoPause();
}
});
@Override
public void onPause() {
super.onPause();
player.pause();
}
private void demoPause(){
player.pause();
Toast.makeText(this, notPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, notPlaying);
}
private void demoPlay(){
player.start();
Toast.makeText(this, isPlaying, Toast.LENGTH_LONG).show();
Log.d(TAG, isPlaying);
}
}