Mediaplayer因未经处理的事件而离开

时间:2017-07-16 11:28:29

标签: android button android-mediaplayer

我发出了几个按键声音,但我无法播放所有按键声音。我已经搜索了一个解决方案,答案似乎是我需要释放()并在之前添加reset()。但是,当我单击按钮两次时,我的应用程序错误显示"不幸的是,该应用已停止"。

我的代码是:

ImageButton kaa = (ImageButton) findViewById(R.id.kaa);
    kaa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(HurufActivity.this);
            View view = getLayoutInflater().inflate(R.layout.huruf_ka, null);

            ImageButton ka = (ImageButton) view.findViewById(R.id.ka);
            ka.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonka.start();
                    buttonka.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion(MediaPlayer mp) {
                            buttonka.reset();
                            buttonka.release();
                        }
                    });

                }})
builder.setView(view);
            AlertDialog dialog = builder.create();
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.show();

        }
    });

1 个答案:

答案 0 :(得分:0)

不要在另一个OnClickListener中设置OnClickListener。

AlertDialog.Builder builder = new AlertDialog.Builder(HurufActivity.this);
View view = getLayoutInflater().inflate(R.layout.huruf_ka, null);
ImageButton ka = (ImageButton) view.findViewById(R.id.ka);

buttonka.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
    if( buttonka != null) {
        if( buttonka.isPlaying()) {
            buttonka.stop();
        }
        buttonka.reset();
        buttonka.release();
        buttonka = null;
        }
    }
});

ka.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        buttonka.start();

    }
});

builder.setView(view);
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();


ImageButton kaa = (ImageButton) findViewById(R.id.kaa);
kaa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

           dialog.show();

    }
});