What is VideView

The VideoView is used to play the video in android.

In my prgram when user open the app its automatically play the video user can stop or again play the video.For this you need to click on the video bellow the video you can find the play button and next or previous also.Pls see the image it clear your doubts.


VideoView:.

In this section i am going to show the videoview with practical example.

First open the android studio and create a project and give the name of your project like VideoViewDemo.

The screenshots of the program is as follows:

media image media image

Drag and drop the one VideoView from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="700dp"
       />
</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity  {
    VideoView videoView;
    MediaController mediaController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        videoView=(VideoView)findViewById(R.id.videoView);
        mediaController=new MediaController(this);
        videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.song);

        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();

    }
}



Explanation:

VideoView videoView:This statement declare the object videoView of VideoView.

MediaController mediaController:This statement declare the object mediaController of MediaController .

videoView=(VideoView)findViewById(R.id.videoView);:This statement initialized the videoView object.

mediaController=new MediaController(this):This statement instantiate the mediaController object.

videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.song):The setVideoPath() set the video path in parameter we need to write "android.resource://"+getPackageName+"/"+R.raw.video.for voideo you simple need to right click on res folder and a dialog box will display take the AndroidResouceDirectory and choose the resource type as raw and then click on finish button.The raw folder display under the res folder then copy and paste the video inside the raw folder.

mediaController.setAnchorView(videoView):Set the view that acts as the anchor for the control view.

videoView.setMediaController(mediaController):The setMediaController() set the media controller.Here the media controller is mediaCotroller object.

videoView.start():The start() play the video.


VideoView

what is video view for video click here

back image                     back image