0

我正在尝试制作一个简单的音乐播放器应用程序,其中包含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);
    }
}
4

2 回答 2

0
player.prepare();

尝试将此行放在 player.start() 之前;

于 2012-07-25T18:07:24.617 回答
0

错误是您没有将小部件设置pausebtnImageButton

这样的事情应该这样做:

pausebtn = (ImageButton)findViewById(R.id.name_of_image_button_in_your_layout_xml);

if (pausebtn != null){
    pausebtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            demoPause();
        }
    });
}
于 2012-07-25T18:11:40.090 回答