Wednesday 17 August 2016

Video View

Video View
VideoView  is a  video player, which is use to play video from your phone, streaming online video and also show image from various source.
It provide various display option like - Scaling & Tinting.
Note: VideoView does not retain its full state when going into the background. In particular, it does not restore the current play state, play position, selected tracks, or any subtitle tracks added via addSubtitleSource(). Applications should save and restore these on their own in onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle).
Code -
<LinearLayout 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=".MainActivity">

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

</
LinearLayout>

Play Video from raw file
1. Create a raw folder in your resource (res).
2. Tocreate folder - Goto "app" -> right click -> directory -> name it "raw" -> OK
code -


VideoView videoView=(VideoView)findViewById(R.id.videoview1);

videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.meinu_peene_de));

videoView.start();

Play Video from external storage
1. save video in your external device
2. name must be small a-z, 0-9 and underscore
3. Permission in android
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Code -
VideoView videoView=(VideoView)findViewById(R.id.videoview1);

videoView.setVideoPath("/sdcard/meinu_peene_de.avi");

videoView.start();

Play Video from youtube
1. Internet Permission Requird
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 
code - 
VideoView videoView=(VideoView)findViewById(R.id.videoview1);

videoView.setVideoURI(Uri.parse("http://abhiandroid.jobxfryqt.netdna-cdn.com/ui/wp-content/uploads/2016/04/videoviewtestingvideo.mp4"));

videoView.start();

1. setVideoPath() - This is used to set the path of video, which take string format.
videoView.setVideoPath("/sdcard/meinu_peene_de.avi");

2. setVideoURI - parse the Uri of video like - youtube or video from raw folder.
raw_folder
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.meinu_peene_de));
youtube
videoView.setVideoURI(Uri.parse("http://abhiandroid.jobxfryqt.netdna-cdn.com/ui/wp-content/uploads/2016/04/videoviewtestingvideo.mp4"));
3. start() - used to start playing video.

videoView.start();
4. MediaController - This provide controls for a MediaPlayer. Typically contains the buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress slider. It takes care of synchronizing the controls with the state of the MediaPlayer.
MediaController window will disappear if left idle for three seconds and reappear when the user touches the anchor view.
code- 
VideoView videoView=(VideoView)findViewById(R.id.videoview1);

videoView.setVideoURI(Uri.parse("http://abhiandroid.jobxfryqt.netdna-cdn.com/ui/wp-content/uploads/2016/04/videoviewtestingvideo.mp4"));

MediaController mediaController=new MediaController(this);

videoView.setMediaController(mediaController);

videoView.start();


5. canPause(): This method will tell whether VideoView is able to pause the video. This method returns a Boolean value means either true or false. If a video can be paused then it returns true otherwise it returns false.
Below we checks whether a video is able to pause or not.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
Boolean canPauseVideo = simpleVideoView.canPause(); // check whether a video is able to pause or not
6. canSeekForward(): This method will tell whether video is able to seek forward. This method returns a Boolean value i.e. true or false. If a video can seek forward then it returns true otherwise it returns false.
Below we checks whether a video is able to seek forward  or not.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
Boolean canSeekForward = simpleVideoView.canSeekForward(); // checks whether a video view is able to seek forward  or not
7. canSeekBackward(): This method will tell whether video is able to seek backward. This method returns a Boolean value i.e. true or false. If a video can seek backward then it return true otherwise it return false.
Below we checks whether a video is able to seek backward  or not.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
Boolean canSeekBackword = simpleVideoView.canSeekBackward(); // checks whether a video view is able to seek backword or not
8. getDuration(): This method is used to get the total duration of VideoView. This methods return an integer value.
Below we get the total duration of a  video view.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
int duration =simpleVideoView.getDuration();// get the total duration of the video
9. getCurrentPosition(): This method is used to get the current position of playback. This method returns an integer value.
Below we get the current position of a playback.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
int currentPosition = simpleVideoView.getCurrentPosition(); // get the current position of the video play back
10. isPlaying(): This method tells whether a video is currently playing or not. This method returns a Boolean value. It returns true if video is playing or false if it’s not.
Below we check whether a video view is currently playing or not
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
Boolean isPlaying = simpleVideoView.isPlaying(); // check whether a video view is currently playing or not
11. stopPlayback(): This method of VideoView is used to stop the video playback.
Below we show how to stop a video in video view.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
simpleVideoView.stopPlayback(); // stop a video 
12. setOnPreparedListener(MediaPlayer.OnPreparedListener): This is a  listener which allows a callback method to be called when the video is ready to play.
Below we show the use of setOnPreparedListener event of a video view.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
 
// perform set on prepared listener event on video view
simpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
 
// do something when video is ready to play
 
}
});
13. setOnErrorListener(MediaPlayer.OnErrorListener): This listener allows a callback method to be called when an error occurs during the video playback.
Below we show the use of setOnErrorListener event of a video view.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
 
// perform set on error listener event on video view
simpleVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
 
 
// do something when an error is occur during the video playback
return false;
}
});
14. setOnCompletionListener(MediaPlayer.OnCompletionListener): This listener allow a callback method to be called when the end of the video is reached.
Below we shows the use of setOnCompletionListener event of a video view.
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // 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
}
});

MediaController In VideoView

MediaController is a class which is used to provide the controls for the video playback. If a video is simply played using the VideoView class then the user will not be given any control over the playback of the video which will run until the end of the video is reached. This issue can be addressed by attaching an instance of the MediaController class to the VideoView instance. The Media Controller will then provide a set of controls allowing the user to manage the playback (such as seeking backwards/forwards and pausing in the video timeline).
Methods Of MediaController:
Let’s we discuss some important methods of MediaController class that may be called in order to control for the playback.
1. setAnchorView(View view): setAnchorView is used to designates the view to which the controller is to be anchored. This controls the location of the controls on the screen.
Below we show how to use setanchorview() method of a MediaController class.
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.setAnchorView(simpleVideoView); // set anchor view for video view
2. show(): This method is used to show the controller on the screen.
Below we 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
3. show(int timeout): This method is used to set the time to show the controller on the screen.
Below we set the time for showing the controller on the screen.
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.show(500); // set the time to show the controller on the screen
4. hide(): This method is used to hide the controls from the screen.
Below we hide the control from the screen
MediaController mediaController = new MediaController(this); // create an object of media controller
mediaController.hide(); // hide the control from the screen
5. isShowing(): This method returns a Boolean value indicating whether the controls are currently visible to the user or not.
Below we checks whether the controls are currently visible or not.
MediaController mediaController = new MediaController(this); // create an object of media controller
Boolean isShowing = mediaController.isShowing(); // checks whether the controls are currently visible or not





























22

No comments:

Post a Comment