我想在对话框中播放声音。对话框中有一个 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);
}
});