2
votes

I came to know that we can play video in texture view. But I saw only how to play videos capturing by camera but I want to play an available video in it to perform any animations.

I have tried this code but I don't know where to place video.mp4

public class MainActivity extends Activity implements
        TextureView.SurfaceTextureListener {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

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

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setSurface(new Surface(surface));

        try {
            mediaPlayer.setDataSource("video.mp4");
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
            int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
        // TODO Auto-generated method stub

    }
}

Please help me by giving any working code. Or if mine was correct then where to place video.mp4 in the folders.

Thanks in advance...

1
what folders are you talking about? - pskink
I tried by copying video.mp4 in raw folder and also in assets folder - Sri
use a full path: something that starts with "/" - pskink

1 Answers

1
votes

Place it in raw folder and try the below one and see if it helps.

public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setSurface(new Surface(surface));
    try {
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_raw_file);
        mediaPlayer.setDataSource(this, video);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}