Wednesday, 10 July 2019

Android VideoView with Multimedia controls

Here is the code for play any video using VideoView and with Media Controller

Add below code in your main_activity.xml

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"    
android:layout_alignParentBottom="true"    
android:layout_centerHorizontal="true"    
android:layout_gravity="center"    
android:layout_marginTop="@dimen/d_10dp"    
android:layout_marginBottom="@dimen/d_10dp">

<VideoView    
android:id="@+id/videoview"    
android:layout_width="match_parent"    
android:layout_height="200dp"   />

</RelativeLayout>




Now add below code in MainActivity.java

define your variable first


VideoView videoView;
MediaController mediaControls;

in onCreateView method add below code
videoView = (VideoView)view.findViewById(R.id.videoview);

add below code where you want to play video

String FullVideopath = "Here is your video path";

videoView.setVideoPath(FullVideopath);
mediaControls = new MediaController(mcontext);
mediaControls.setAnchorView(videoView);
videoView.setMediaController(mediaControls);
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override    public void onPrepared(MediaPlayer mp) {
        mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override  public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                videoView.setMediaController(mediaControls);
                mediaControls.setAnchorView(videoView);
            }
        });
    }
});

viewvideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override    public void onCompletion(MediaPlayer mp) {
        Toast.makeText(mcontext, "Video over...!!!", Toast.LENGTH_LONG).show();
    }
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    @Override    public boolean onError(MediaPlayer mp, int what, int extra) {
        Toast.makeText(mcontext, "Oops An Error Occur While Playing Video...!!!", 
Toast.LENGTH_LONG).show(); 
return false;
    }
});}

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...