Monday, 7 September 2015

Video Streaming

Hello Guys,

Today I share Video Streaming implementation in android apps with you.

I already explained the meaning of streaming in my previous post Audio Streaming.
If anybody need to know again then refer my previous post.

All of you know that when you play any video in youtube player then its done buffering some part for few minutes then play, again buffering some part and play.

This process is going till the end of video.

Here I provide you sample code for Video Streaming which is fully done buffering first
then play continuously.

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

NOTE: YOU MUST NEED TO TEST THIS DEMO APP INTO YOUR DEVICE ONLY.

Sample Code:

File: MainActivity.java

package com.sneha.videostreamtutorial;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity 
{
Button button;

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

button = (Button) findViewById(R.id.MyButton);

button.setOnClickListener(new OnClickListener() 
{
public void onClick(View arg0) 
{
Intent myIntent = new Intent(MainActivity.this, VideoViewActivity.class);
startActivity(myIntent);
}
});
}
}
   
File: VideoViewActivity.java

package com.sneha.videostreamtutorial;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Window;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewActivity extends Activity 
{
ProgressDialog pDialog;
VideoView videoview;

// Insert your Video URL

String VideoURL = "https://ia700401.us.archive.org/19/items /                                                               ksnn_compilation_master_the_internet/ ksnn_compilation_master_the_internet_512kb.mp4";

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.videoview_main);

videoview = (VideoView) findViewById(R.id.VideoView);

pDialog = new ProgressDialog(VideoViewActivity.this);
pDialog.setTitle("Android Video Streaming Tutorial");
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();

try 
{
// Start the MediaController
MediaController mediacontroller = new MediaController(VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);

// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);

}
catch (Exception e) 
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}

videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() 
{
public void onPrepared(MediaPlayer mp) 
{
pDialog.dismiss();
videoview.start();
}
});
}
}


Output:











































































Download Full Source Code From Here: VideoStreaming

Happy Coding...!!!

1 comment:

  1. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.Streaming De VĂ­deo Ilimitado

    ReplyDelete