Skip to content Skip to sidebar Skip to footer

Sound With Animation In Android

Hi I am making a slide show.images slide automatically in slide show these images are user selected images stored in array.now i want to plat user selected audio along with animati

Solution 1:

You can play sound and stop it in AnimationListener's method as shown here:

MediaPlayerplayer= MediaPlayer.create(SlideShow.this, uriaudio); 
player.setLooping(false); // Set looping 
player.setVolume(100,100); 
finalintmPlayerLength=0;

anim.setAnimationListener(newAnimationListener() {

    @OverridepublicvoidonAnimationStart(Animation animation) {
        player.seekTo(mPlayerLength);
        player.start();
    }

    @OverridepublicvoidonAnimationRepeat(Animation animation) {
    }

    @OverridepublicvoidonAnimationEnd(Animation animation) {
         if(player.isPlaying()){
            player.stop();
            mPlayerLength = player.getCurrentPosition();
        }
    }
};

Hope it helps.

EDIT:

Yes, final modifier will not let you change it later. Sorry for that. I didn't run the code. Here is the alternate: You can implement AnimationListener in your activity. So you will not need to keep the variable as final to access it in methods:

publicclassMyActivityextendsActivityimplementsAnimationListener{

    intmPlayerLength=0;  //global variable

    anim.setAnimationListener(this);       

    publicvoidonAnimationStart(Animation animation) {
        // TODO Auto-generated method stub
        player.seekTo(mPlayerLength);
        player.start();
    }

    @OverridepublicvoidonAnimationEnd(Animation animation) {
        // TODO Auto-generated method stubif(player.isPlaying()){
            player.stop();
            mPlayerLength = player.getCurrentPosition();
        }
    }

}

Hope this is clear and solves issue :)

Post a Comment for "Sound With Animation In Android"