Sound with animation in android

2019-08-30 09:47发布

问题:

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 animation.but i don't know how to do this.the images animation is working fine but canot play sound at background.here is my code

final Handler mHandler = new Handler();

            // Create runnable for posting
            final Runnable mUpdateResults = new Runnable() {
                public void run() {

                        AnimateandSlideShow();
                             }
            };

           // mHandler.postDelayed(mUpdateResults, 300);
            int delay =300; // delay for 1 sec.

            int period = 9000; // repeat every 4 sec.

            Timer timer = new Timer();

            timer.scheduleAtFixedRate(new TimerTask() {


            public void run() {

                 mHandler.post(mUpdateResults);

            }

            }, delay, period);

            ////////end animation//////////////////

here is the animatedslideshow function:

 private void AnimateandSlideShow() {
         // playsound();
         //  sound =new AnimationSound(SlideShow.this,uriaudio);
         //  sound.startsound();

           iv=(ImageView)findViewById(R.id.anim_view);

          iv.setImageURI(Uri.parse(allimgs.get(count%allimgs.size()).toString()));

          count++;
           anim = AnimationUtils.loadAnimation(SlideShow.this, R.anim.fade_in);

           // load your desire animation.
            iv.startAnimation(anim);

    }

and playsound:

private void playsound()
      {
          MediaPlayer player = MediaPlayer.create(SlideShow.this, uriaudio); 
            player.setLooping(false); // Set looping 
            player.setVolume(100,100); 
            player.start(); 

      }

i don't know where to put the playsound function in handler so that audio starts with animation.

回答1:

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

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

anim.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        player.seekTo(mPlayerLength);
        player.start();
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(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:

public class MyActivity extends Activity implements AnimationListener{

    int mPlayerLength = 0;  //global variable

    anim.setAnimationListener(this);       

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

    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub
        if(player.isPlaying()){
            player.stop();
            mPlayerLength = player.getCurrentPosition();
        }
    }

}

Hope this is clear and solves issue :)