Play video from url using VideoView in android



in this post you will learn how to play video from video url using Video View in your application, and hear i am not use MediaController but if you use then you can perform operation like play,pause,stop,e.t.c, video.

Class File : VideoViews.java
package com.materialexample;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.VideoView;
import in.co.bhadreshtech.materialexample.R;
public class VideoViews extends AppCompatActivity {
    ProgressBar progressBar = null;
    VideoView videoView1 = null;
    String videoUrl = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
    Context context = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_views);
        context = null;
        videoView1 = (VideoView) findViewById(R.id.video_view1);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        Uri videoUri = Uri.parse(videoUrl);
        videoView1.setVideoURI(videoUri);
        videoView1.start();
        progressBar.setVisibility(View.VISIBLE);
        videoView1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1,
                                                   int arg2) {
                        // TODO Auto-generated method stub
                        progressBar.setVisibility(View.GONE);
                        mp.start();
                    }
                });

            }
        });
    }
}


Layout File  : VideoViews.java

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.materialexample.VideoViews">

    <VideoView
        android:id="@+id/video_view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="50dp"
        android:layout_height="50dp" />
</RelativeLayout>

No comments: