0

アラートボックスが表示されているときにサウンド/オーディオを再生しています。

AlertdialogのPositiveまたはNegativeボタンを押した場合に対応したいのですが、それまでに音声が完成していない場合は、音声の再生を止めてほしいです。

この特定の状態を処理するにはどうすればよいですか?

コード:

        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 に答える