简体   繁体   中英

How to use the VideoView .isPlaying() method?

My Aim is to play a repeating animation for the duration the video is played, as I am using a VideoView I thought that I might as well take advantage of the .isPlaying method to resolve implement this however the while loop does not execute at all

    Floater0 = (ImageView) findViewById(R.id.Floater0);
    AdPlayer0 = (VideoView) findViewById(R.id.AdPlayer);

    ConvertedSource = Uri.parse("android.resource://"+ getPackageName()+ "/" + R.raw.kitkat);
    AdPlayer0.setVideoURI(ConvertedSource);
    AdPlayer0.start();

    while (AdPlayer0.isPlaying()){
        TranslateAnimation animation = new TranslateAnimation(answer,  answer, 0,  height * -1); // xfrom , xto, y from,y to
        animation.setDuration(5000);  // animation duration influences the speed of full execution
        Floater0.setVisibility(View.VISIBLE); //show it
        Floater0.startAnimation(animation);  // start animation

    }

The video plays successfully but animations do not appear, I have made sure that the animation is implemented and having correctly simply by placing it out side the braces of the loop, however this is not the aim as I want to repeatedly play animations when the video is played!

 AdPlayer0.start();

 while (AdPlayer0.isPlaying()){
        TranslateAnimation animation = new TranslateAnimation(answer,  answer, 0,  height * -1); // xfrom , xto, y from,y to
        animation.setDuration(5000);  // animation duration influences the speed of full execution
        Floater0.setVisibility(View.VISIBLE); //show it
        Floater0.startAnimation(animation);  // start animation

    }

Maybe the video don't have time to load, after the start your are directly calling the while, but isPlaying should return false maybe as the video is not started yet. Moreover in your while block your creating a new animation, it will result to create multiple animation when the video is still playing...

TranslateAnimation animation = new TranslateAnimation(answer,  answer, 0,  height * -1); // xfrom , xto, y from,y to
animation.setDuration(5000);
Floater0.setVisibility(View.VISIBLE); //show it
Floater0.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
    if (AdPlayer0.isPlaying()) {
        Floater0.startAnimation(animation);
    }
}

for your video view use setOnCompletionListener for find out to video finish playing. Inside this listener stop the animation and hide the Floater0.setVisibility(View.GONE);

I think you just need to remove the while (AdPlayer0.isPlaying()){

add animation.setRepeatCount(Animation.INFINITE);

and on

    animation.setAnimationListener(new AnimationListener() {
    @Override
    public void onAnimationEnd(Animation arg0) {
        Floater0.stopAnimation(animation);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM