Android音乐播放器应用无法打开

时间:2012-07-25 17:55:17

标签: android

我正在尝试使用简单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);
    }
}

2 个答案:

答案 0 :(得分:0)

player.prepare();

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

之前

答案 1 :(得分:0)

错误是您没有将pausebtn设置为ImageButton小部件。

这样的事情应该这样做:

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();
        }
    });
}