How do you rate this blog

Saturday, July 12, 2014

Video Streaming in Android

In this post we will see how to stream videos in the applications in android.

To implement this the logic is

1) Allow internet access to the application
2) Add video control on the view
3) Add the media control to playback control to pause, play, move forward or backward.

We need a link from which we can stream videos. We will use the link below and stream video from this link
https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4

We are not using a youtube link in this post since those videos need to be accessed through the YouTube API.

So Lets get started, create a new project in eclipse using Android Jelly Bean and create a blank activity. Now we will implement logic which was described above-

1) Allow internet access to the application


  Since the application will stream video it needs internet access, so add the below code in the androidmanifest file allow the application to access the internet

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

2) Add video control on the view


 Now we need to add the video control on the application

a) Go to the layout file and the below code

    <VideoView
        android:id="@+id/myVideoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

b) Go to the java file in src file and add bind the layout control with the Uri as shown below

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

        //binding the view
        VideoView myvid=(VideoView) findViewById(R.id.myVideoView);
        //to set the uri that should be streamed in the video
        String videolink="https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
        Uri videoUri=Uri.parse(videolink);

        //add the control on the view
        myvid.setMediaController(mycontroler);

        //to play the video
        myvid.setVideoURI(videoUri);
        myvid.start();
       
    }

3) Add the media control


   Now we have added the player in the application, but we need to add the playback controls in the video control which will allow you to perform basic operations like play, pause, move forward or back.

        //play back controllers
        MediaController mycontroler=new MediaController(this);
        mycontroler.setAnchorView(myvid);
Add this code in the onCreate function described above, the final code will be

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videostreamer);
        //binding the view
        VideoView myvid=(VideoView) findViewById(R.id.myVideoView);
        //to set the uri that should be streamed in the video
        String videolink="https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
        Uri videoUri=Uri.parse(videolink);
        //play back controllers
        MediaController mycontroler=new MediaController(this);
        mycontroler.setAnchorView(myvid);
        //add the control on the view
        myvid.setMediaController(mycontroler);
        //to play the video
        myvid.setVideoURI(videoUri);
        myvid.start();
       
    }


Now lets run the application, the output will as shown in the snapshot below

No comments:

Post a Comment