Play video from resource file in Android using VideoView.

Hear is the code demo for play video from android resource "raw" directory,


Layout code : activity_video_views.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.materialexample.VideoViews">
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>


Activity Code : VideoViews

package com.materialexample;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView;
import in.co.materialexample.R;

public class VideoViews extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_views);
        VideoView videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        int resID = getResources().getIdentifier("video1", "raw", this.getPackageName());
        Uri video1 = Uri.parse("android.resource://" + this.getPackageName() + "/" + resID); //do not add any extension
        videoView.setMediaController(null);
        videoView.stopPlayback();
        videoView.setVideoURI(video1);
        // Log.i("video1..", ".." + video1 + ".." + resID);
        videoView.requestFocus();
        videoView.start();

    }
}

No comments: