Play Video File Android Example

Now, Moving forward in Android Examples ,Today you are going to learn a new Android Example of Play Video File Android Example with code . If you are a beginner Please go through the tutorial from very start from here .Play Video File in Android is very simple and easy to code .You can also incorporate  Play Video File Android Example in your project or app. For Play Video File Android Example ,we used Android Studio you can use any other Android IDE as well.

Play Video File Android Example
Play Video File Android Example






Steps of Play Video File Android Example are  explained below :




Step 1 : Create a new Project -PlayVideo and Activity 

Play Video File Android Example
Play Video File Android Example

Step 2 : Create Activity xml 
You 
Add a button and Video View in activity xml file for Play Video File Android Example with code.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:orientation="vertical"    
         android:layout_width="fill_parent"   
         android:layout_height="fill_parent"    >

    <Button android:id="@+id/button"
        android:layout_width="fill_parent"     
        android:layout_height="wrap_content"   
        android:text="PLAY Video"        />


    <VideoView 
          android:id="@+id/videoview"
          android:layout_width="fill_parent"
         android:layout_height="wrap_content"        />


</LinearLayout>

Step 3 : Project Structure

For Play Video File You need to place a video file ,as I placed test.mp4 file under raw directory as shown below :

Play Video File Android Example
Play Video File Android Example



Step 4 : Create Java Activity


package javainhouse.com.playvideo;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class PlayVideoActivity extends Activity implements SurfaceHolder.Callback{


    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean pausing = false;;


    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

        Button buttonPlayVideo = (Button)findViewById(R.id.button);

        getWindow().setFormat(PixelFormat.UNKNOWN);

        //Displays a video file.        VideoView mVideoView = (VideoView)findViewById(R.id.videoview);



        Uri uri = Uri.parse("android.resource://javainhouse.com.playvideo/raw/"+R.raw.test);

        mVideoView.setVideoURI(uri);
        mVideoView.requestFocus();
        mVideoView.start();



        buttonPlayVideo.setOnClickListener(new Button.OnClickListener(){

            @Override            public void onClick(View v) {

                // VideoView refference see main.xml                VideoView mVideoView = (VideoView)findViewById(R.id.videoview);



                Uri uri = Uri.parse("android.resource://javainhouse.com.playvideo/raw/test.mp4");
                mVideoView.setVideoURI(uri);
                mVideoView.requestFocus();
                mVideoView.start();


            }});
    }

    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
    }

    @Override    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    @Override    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
}

Post a Comment