WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

VideoView Android


VideoView is used to display a video file.

In android Applications should save and restore these in onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle)


VideoView code In XML
<VideoView
android:id="@+id/demoVideoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Method VideoView

setVideoUri(Uri uri) This method is used to set the absolute path of the video file which is going to be played. This method takes a Uri object as an argument.
VideoView demoVideoView = (VideoView) findViewById(R.id.demoVideoView);
simpleVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.testvideo));

Media Controller In VideoView

There are following methods to control to set up videoview . setAnchorView(View view) It used to designates the view to which the controller is to be anchored. This controls the location of the controls on the screen.
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.setAnchorView(demoVideoView); // set anchor view for video view
show()show() method is used to show the controller on the screen.
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.show(); // show the controller on the screen
show(int timeout) show() method is used to show time interval the controller on the screen
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.show(1000); // set the time to show the controller on the screen
hide() method is used to hide the controller on the screen
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.hide(); // hide the control from the screen
isShowing() method returns a Boolean value indicating whether the controls are currently visible to the user or not.0 or 1
MediaController mediaController = new MediaController(this); // create an object of media controller
Boolean isShowing = mediaController.isShowing(); // checks whether the controls are currently visible or not


Example of videoview online

go manifest file and gives permission internet put code

<uses-permission android:name="android.permission.INTERNET" />
then VideoVideo XML code in activity_main.xml or activity.xml
package welookups.com.videofromwebsource;

import android.app.ProgressDialog;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Uri uri = Uri.parse("/ui/wp-content/uploads/2018/06/videoviewtestingvideo.mp4");
            VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
            simpleVideoView.setVideoURI(uri);
            simpleVideoView.start();
    }
}
setMediaController(MediaController controller)This method of VideoView is used to set the controller for the controls of video playback.
// create an object of media controller
MediaController mediaController = new MediaController(this);
// initiate a video view
VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); 
// set media controller object for a video view
simpleVideoView.setMediaController(mediaController);

start() This method of VideoView is used start the playback of video file.

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
demoVideoView.start(); // start a video

pause() This method of VideoView is used pause the current playback.

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
demoVideoView.pause(); // pause a video

canPause() This method will tell whether VideoView is able to pause the video

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
Boolean canPauseVideo = demoVideoView.canPause(); // check whether a video is able to pause or not

canSeekForward() This method of tell whether VideoView is seek forward.return on Boolean value i.e. true or false

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
Boolean canSeekForward = simpleVideoView.canSeekForward(); // checks whether a video view is able to seek forward  or not

getDuration() This method of VideoView is used get the total duration. This methods return an integer value.


VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
int duration =simpleVideoView.getDuration();// get the total duration of the video

isPlaying() This method tells whether a video is currently playing or not.This method returns a Boolean value.

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
Boolean isPlaying = demoVideoView.isPlaying(); // check whether a video view is currently playing or not

stopPlayback() This method of VideoView is used to stop the video playback.

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
demoVideoView.stopPlayback(); // stop a video

setOnCompletionListener(MediaPlayer.OnCompletionListener) This listener allow a callback method to be called when the end of the video is reached.

VideoView simpleVideoView = (VideoView) findViewById(R.id.demoVideoView); // initiate a video view
// perform set on completion listener event on video view
simpleVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// do something when the end of the video is reached
}
});