当显示警告框时,我正在播放一些声音/音频。
我想处理当我按下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();
}
});
答案 0 :(得分: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();
}
});