在Android的动画声音 [英] Sound with animation in android

查看:171
本文介绍了在Android的动画声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想提出一个幻灯片show.images在幻灯片放映自动滑行这些图像存储在array.now用户选择的图像,我想沿着开发平台用户选择的音频animation.but我不知道如何做到这一点。图像动画工作正常,但在canot玩background.here声音是我的code

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//////////////////

这里是animatedslideshow功能:

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

    }

和playsound:

and playsound:

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

      }

我不知道放在哪里playsound功能的处理程序,以便音频,动画开始。

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

推荐答案

您可以播放声音和如下所示停止在AnimationListener的方法:

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

希望它帮助。

编辑:

最后修改不会让你以后改变它。对不起这一点。我没有运行code。这里是交替:您可以实施 AnimationListener 在您的活动。这样你就不会需要保持变量,最后访问它的方法:

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

这篇关于在Android的动画声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆