Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Monday, 15 February 2016

Video-Play Live Streaming in Android | Play Video in Android Using Remote URL

Hello Friends,
Today i am sharing post regarding live streaming from server to android application.




Note : There are so many different way to do live streaming in android application but in android only 3gp and mp 4 are playing smoothly or it may have exception for large resolution video also. we have to implement different approach for rest live streaming.

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity
{

       private static ProgressDialog progressDialog;
       String videourl="You video url"; //It should be 3gp or mp4
       VideoView videoView ;

       @Override
       protected void onCreate(Bundle savedInstanceState)
       {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

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

              progressDialog = ProgressDialog.show(MainActivity.this"""Buffering video...",true);
              progressDialog.setCancelable(false);


              PlayVideo();
       }

       private void PlayVideo()
       {
              try
              {   
                     getWindow().setFormat(PixelFormat.TRANSLUCENT);
                     MediaController mediaController = newMediaController(MainActivity.this);
                     mediaController.setAnchorView(videoView);        

                     Uri video = Uri.parse(videourl);          
                     videoView.setMediaController(mediaController);
                     videoView.setVideoURI(video);
                     videoView.requestFocus();           
                     videoView.setOnPreparedListener(new OnPreparedListener()
                     {

                           public void onPrepared(MediaPlayer mp)
                           {                
                                  progressDialog.dismiss();  
                                  videoView.start();
                           }
                     });

              }
              catch(Exception e)
              {
                     progressDialog.dismiss();
                     System.out.println("Video Play Error :"+e.toString());
                     finish();
              }

       }

}
XML Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <VideoView
            android:id="@+id/video_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true" >
        </VideoView>
    </RelativeLayout>

</LinearLayout>

permission<uses-permission android:name="android.permission.INTERNET" />

More Ref Link:

No comments:

Post a Comment