当我打开我的片段时,它包含10个imageViews,当我长时间点击每个imageView时,它会播放4个音频和弹出警报框,当第一个音频完成第二个播放时将播放,然后播放第五个。当第5个音频完成对话框将被解雇。
case R.id.pg10_3: {
final AlertDialog.Builder mbuilder = new AlertDialog.Builder(getActivity());
View mview = getLayoutInflater().inflate(R.layout.page10_popup, null);
p10tajweed_letter = (TextView) mview.findViewById(R.id.tajweed_heading);
p10first_letter_a = (TextView) mview.findViewById(R.id.p10_1la_pop);
p10first_letter_b = (TextView) mview.findViewById(R.id.p10_1lb_pop);
p10first_letter_c = (TextView) mview.findViewById(R.id.p10_1lc_pop);
p10first_letter_d = (TextView) mview.findViewById(R.id.p10_1ld_pop);
p10first_letter_e = (TextView) mview.findViewById(R.id.p10_1le_pop);
disable = (TextView) mview.findViewById(R.id.p10_1lf_pop);
p10first_letter_a.setVisibility(View.VISIBLE);
p10first_letter_b.setVisibility(View.VISIBLE);
p10first_letter_c.setVisibility(View.VISIBLE);
p10first_letter_d.setVisibility(View.VISIBLE);
p10first_letter_e.setVisibility(View.VISIBLE);
p10tajweed_letter.setVisibility(View.VISIBLE);
mbuilder.setView(mview);
final AlertDialog dialog = mbuilder.create();
dialog.show();
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
}
});
if (mp.isPlaying() || mp1.isPlaying() || mp2.isPlaying() || mp3.isPlaying() || mp4.isPlaying() || mp5.isPlaying()) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
clearcolor();
}
try {
if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 0 && UtilityMethods.GetPreferences("check", getActivity()) == false) {
try {
if (mp.isPlaying() || mp1.isPlaying() || mp2.isPlaying() || mp3.isPlaying() || mp4.isPlaying() || mp5.isPlaying()) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
clearcolor();
}
mp.reset();
p10first_letter_a.setTextColor(Color.BLACK);
afd = getActivity().getAssets().openFd("q1p10_1la.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
p10first_letter_a.setTextColor(Color.parseColor("#FFA500"));
p10first_letter_b.setTextColor(Color.BLACK);
mp1.reset();
try {
if (mp.isPlaying() || mp1.isPlaying() || mp2.isPlaying() || mp3.isPlaying() || mp4.isPlaying() || mp5.isPlaying()) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
clearcolor();
}
mp1.reset();
afd = getActivity().getAssets().openFd("q1p10_1lb.mp3");
mp1.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp1.prepare();
mp1.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
p10first_letter_b.setTextColor(Color.parseColor("#32CD32"));
p10first_letter_c.setTextColor(Color.BLACK);
p10first_letter_d.setTextColor(Color.BLACK);
try {
if (mp.isPlaying() || mp1.isPlaying() || mp2.isPlaying() || mp3.isPlaying() || mp4.isPlaying() || mp5.isPlaying()) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
clearcolor();
}
mp2.reset();
afd = getActivity().getAssets().openFd("q1p10_1lc.mp3");
mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp2.prepare();
mp2.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
p10first_letter_c.setTextColor(Color.parseColor("#ffff1521"));
p10first_letter_d.setTextColor(Color.parseColor("#FFA500"));
p10first_letter_e.setTextColor(Color.BLACK);
try {
if (mp.isPlaying() || mp1.isPlaying() || mp2.isPlaying() || mp3.isPlaying() || mp4.isPlaying() || mp5.isPlaying()) {
mp.stop();
mp1.stop();
mp2.stop();
mp3.stop();
mp4.stop();
mp5.stop();
clearcolor();
}
mp3.reset();
afd = getActivity().getAssets().openFd("q1p10_1ld.mp3");
mp3.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp3.prepare();
mp3.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
p10first_letter_e.setTextColor(Color.parseColor("#32CD32"));
dialog.dismiss();
}
});
}
这是我的第一个imagView长按监听器代码。问题是当我第一次点击任何imageView并立即触摸警报对话框外,对话框解除但剩余的音频播放,当我再次重复此操作时,对话框解除并且音频停止。第一次发生了什么?
答案 0 :(得分:0)
删除此行:
dialog.setCanceledOnTouchOutside(true);
答案 1 :(得分:0)
问题在于调用mediaPlayerObj.prepare()
。
基于documentation:
对prepare()的调用可能需要很长时间才能执行,因为它 可能涉及提取和解码媒体数据
在调用prepare()
之前显示您的对话框。只要在对话框外单击,对话框就会被取消并尝试stop()
媒体播放器。
基于mediaPalyerObj.stop()
的文档:
播放开始或暂停后停止播放。
由于您的播放器正忙于准备音频,因此它不会立即“启动”。因此,onstop()
无效。
但是第二次,您的媒体播放器已经启动了音频,stop()
方法将再次生效。
查看您的代码,音频与apk捆绑在一起。把它放在res/raw/
而不是断言。
如果您的音频可用作本地原始资源(res / raw /),则无需拨打prepare()
。
“原始”资源是系统不会尝试解析的文件 任何特殊的方式。
您可以参考官方文件here。