0

显示警报框时,我正在播放一些声音/音频。

我想处理当我按下 Alertdialog 的正或负按钮时的情况,如果到那时音频还没有完成,我希望音频停止播放。

如何处理这种特定情况?

代码:

        AlertDialog.Builder alertdialog = new AlertDialog.Builder(
                Activity.this);
        alertdialog.setTitle("Title ");
        alertdialog.setMessage("The MEssage ");


        LayoutInflater layoutinf= LayoutInflater.from(Activity.this);
        final View view = layoutinf.inflate(R.layout.layoutfile, null);
        alertdialog.setView(view);
        alertdialog.setPositiveButton("Button1",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        //do something 
                    }
                });
        alertdialog.show();

                MediaPlayer mp = MediaPlayer.create(this, R.raw.file_to_play);
                mp.start();
                mp.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }

                });
4

1 回答 1

1

我认为这对你来说应该没问题。

      AlertDialog.Builder alertdialog = new AlertDialog.Builder(
                Activity.this);
        alertdialog.setTitle("Title ");
        alertdialog.setMessage("The MEssage ");

        final  MediaPlayer mp = MediaPlayer.create(this, R.raw.file_to_play);
        LayoutInflater layoutinf= LayoutInflater.from(Activity.this);
        final View view = layoutinf.inflate(R.layout.layoutfile, null);
        alertdialog.setView(view);
        alertdialog.setPositiveButton("Button1",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int which) {
                        if( mp !=null && mp.isPlaying()){
                                 mp.stop();
                           }
                    }
                });
        alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

          @Override
    public void onClick(DialogInterface arg0, int arg1) {
              if( mp !=null && mp.isPlaying()){
                           mp.stop();
                        }                       
    }
         });
        alertdialog.show();


                mp.start();
                mp.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }

                });
于 2013-03-23T18:48:02.553 回答