Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VideoView Animation

I am using a videoview with an animation (video.setAnimation(slideinRight);) Everything works ok except that on the transition, only the layout of the videview is animating, not the video. When the translate animation occurs, i see a box move and mask over my video but the video never moves with it. I am at a loss on what to do now.

like image 987
newdev Avatar asked Jul 19 '11 12:07

newdev


People also ask

What is VideoView?

android.widget.VideoView. Displays a video file. The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.


3 Answers

I'm currently trying to put animations on VideoView.

If you look at Android source Code, VideoView is basically a SurfaceView, coupled with a MediaPlayer, with a basic management of player's state machine.

Actually, the real 'draw' work seems to be handled by native methods in mediaPlayer (a.k.a. the real player engine implementation on your android device)

We've tested animations on different devices, and found that VideoView's underlying video player behavior/implementation isn't the same among different Android Devices :

  • some devices handle player's view animations correctly
  • others DON'T, and just display blur, black screen, or buggy display...

On top of that, VideoView seems to be written directly on memory, so any 'workaround' (like putting an opaque view in front, and setting an animation on that view) doesn't seem to work.

I'd be glad to have others feedback on this :)

like image 94
Vinzzz Avatar answered Oct 20 '22 10:10

Vinzzz


Sorry for answering this rather late, but for what it's worth. and if you're looking only to use a 'slide' animation. Try putting the videoview in a layout and animating the layout.

The way i have it set up in my code is;

AbsoluteLayout Animationlayout = (AbsoluteLayout)findViewById(R.string.frontlayout);
VideoView pvv = new VideoView(getApplicationContext());
pvv.getHolder().addCallback(this);
Animationlayout.addView(pvv);
// load video data in pvv.

Then where you want to animate your videoview to slide;

Animationlayout.animate().xBy(25).setDuration(500).setInterpolator(new BounceInterpolator());

Note that this is the 3.1 animation system.

Not sure of the classic 2.1 way of animating is going to work like this, but it should serve the same.

Stuff like rotating/scaling the layout won't work. Panning the layout around and fading it seem to be the only few things that do work.

like image 38
Zack Avatar answered Oct 20 '22 09:10

Zack


Simply use TextureView:

More reference on TextureView here.

I have done ZoomIn - ZoomOut animation on TextureView.

Add animation xml in Res -> anim folder.

zoom_in_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <scale
        android:duration="1000"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.2"
        android:toYScale="1.2" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
            android:duration="1000"
            android:fillBefore="false"
            android:fromXScale="1.2"
            android:fromYScale="1.2"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="500"
            android:toXScale="1.0"
            android:toYScale="1.0" />

    </set>
</set>

texture_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextureView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textureView"
    android:layout_margin="50dp"
    android:background="@android:color/darker_gray"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

</TextureView>

TextureViewActivity.java:

import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import java.io.IOException;


public class TextureViewActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener  {

    private TextureView textureView;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.texture_layout);

        textureView = (TextureView)findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);

        textureView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation zoomAnimation = AnimationUtils.loadAnimation(TextureViewActivity.this, R.anim.zoom_in_out);
                textureView.startAnimation(zoomAnimation);
            }
        });


    }

    private String getVideoPath(){
        return "android.resource://" + getPackageName() + "/" + R.raw.promovideo;
    }


    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        Surface surface = new Surface(surfaceTexture);
        try {
            mMediaPlayer= new MediaPlayer();
            mMediaPlayer.setDataSource(TextureViewActivity.this, Uri.parse(getVideoPath()));
            mMediaPlayer.setSurface(surface);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            mMediaPlayer.setLooping(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}

Done

like image 1
Hiren Patel Avatar answered Oct 20 '22 10:10

Hiren Patel