我想在对话框中播放声音。对话框中有一个gif动画,现在我也想在其中添加声音。我正在尝试此代码,但没有帮助。我只能够播放gif动画。请帮忙。
ImageButton bicycle = (ImageButton)findViewById(R.id.imageButton1);
bicycle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Vehicles.this);
WebView view = new WebView(Vehicles.this);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
builder.setView(view);
//builder.create().show();
final Dialog d = builder.create(); // save a Dialog reference here
d.show();
view.loadUrl("file:///android_asset/bicycle.gif");
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
mMediaPlayer = new MediaPlayer();
mMediaPlayer = MediaPlayer.create(Vehicles.this, R.raw.bicycle);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
soundIsPlaying = true;
new Handler().postDelayed(new Runnable() {
public void run() {
soundIsPlaying = false;
}
}, PLAYING_TIME_OF_SOUND);
gifIsPlaying = true;
new Handler().postDelayed(new Runnable() {
public void run() {
gifIsPlaying = false;
//finish();
if (d.isShowing()) { // dismiss dialog if still showing
d.dismiss();
}
}
}, PLAYING_TIME_OF_GIF);
}
});
最佳答案
您应该在MediaPlayer
中使用mediaPlayer.release()
释放setOnCompletionListener
的资源
关于android - 在对话框中播放声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19698399/