Meghs
Meghs

Reputation: 269

Play sound in dialog box

I want to play sound in dialog box. There is a gif animation in dialog box and now i want to put a sound too in it. I am trying this code but it isn't that helpful. I am just able to play gif animation. Please help.

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);

        }
    });

Upvotes: 1

Views: 2348

Answers (2)

Linga
Linga

Reputation: 10553

You should release the resources of the MediaPlayer with mediaPlayer.release() in setOnCompletionListener

Upvotes: 0

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this way

mMediaPlayer = MediaPlayer.create(Vehicles.this, R.raw.bicycle);

Upvotes: 1

Related Questions