7 Mar 2012

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 = new MediaController(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:

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)

55 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi,
    it is not show progressDialog........

    ReplyDelete
  3. are you getting any error?

    ReplyDelete
    Replies
    1. hello this is ram,we are using rest full web server in my android application.

      please help me how to upload video from mobile application to server.connect with me ram.mohan375@gmail.com

      Delete
    2. hello this is ram,we are using rest full web server in my android application.

      please help me how to upload video from mobile application to server.connect with me ram.mohan375@gmail.com

      Delete
  4. Please see and implement carefully, it will work nice.

    ReplyDelete
  5. its not showing nythng..shl i add somthing i mean ny class or nythng..

    ReplyDelete
  6. i have change some code now you can try it, is it working on not, and make sure for internet permission on AndroidManifest.xml file.

    ReplyDelete
  7. Its working fine for me. Thanks

    ReplyDelete
  8. Hi Hasmuk,.
    Please tell How to play RTMP or HLS streaming video in ur code,..I tried ur code but i cant play RTMP,.
    what am getting is "Sorry, This video cant be played"

    please tell me what i have to do

    Thank you.

    ReplyDelete
    Replies
    1. Hello,

      Please try following ways.
      1. Device:
      Replace Http with Http live
      eg.path=path.replace("http","httplive");

      For:
      2. Tablets:
      For tab Http is fine

      Note: Set android:hardwareAccelerated="true" in android AndroidManifest.xml file.

      Delete
    2. hai hasmukh can u pls tell wer i want to insert path=path.replace("http","httplive")
      or is ter anything to add in tat.
      and in manifest file under which tag, android:hardwareAccelerated="true" this should be placed...
      kindly reply me fast

      Delete
  9. Please Contact me @

    vsamurali.rick@gmail.com

    please am awaits to hear u,.

    ReplyDelete
  10. Its not working showing dialog that "this video can not play". I tried for "http://www.nasa.gov/multimedia/nasatv/NTV-Public-IPS.m3u8"

    ReplyDelete
    Replies
    1. Hello,

      .m3u8 url is directly not working in above code that code is used only if you have .3gp or .mp4 files.

      you have apply following way:

      Please try following ways.
      1. Device:
      Replace Http with Http live
      eg.path=path.replace("http","httplive");

      For:
      2. Tablets:
      For tab Http is fine

      Note: Set android:hardwareAccelerated="true" in android AndroidManifest.xml file.

      Delete
    2. hi Hasmukh,.

      Am using vitamio player to play my m3u8 stream its working perfect,..but after 20 min its got freeze on Android box, but ion emulator its working fine,.

      pls tel wat might b the problem,.if u need code for the clear idea i'll send u pls give me ur contact.

      thank you,.

      Delete
    3. Is there any other ways to play m3u8 stream in android,.pls tell me,.its urgent
      thanks,

      murali.

      Delete
  11. is it works on mjpeg webcam format ??thanks

    ReplyDelete
  12. but after few secs the screen slipt into two parts.
    do u have any idea?

    ReplyDelete
  13. Hello,

    Sound is played but no video. Please advice.

    ReplyDelete
  14. I'm trying to play a video saved from Ustream, example this http://www.ustream.tv/recorded/15480382, but the on screen appears "Can't Play this video", any suggestions?

    ReplyDelete
    Replies
    1. First check video formate and Video resolution.
      It depend on video format in android phone, generally in android 3gp formate is good to play video, if device like s3, s4 then HD video play other wise it will not play. and need convert in .3gp format.

      Delete
  15. This comment has been removed by the author.

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. I was trying to add .mp4 video from url but not showing video although there was no error, no exception, audio was 'ok' but not displaying video :( plxx help

    ReplyDelete
    Replies
    1. if you are using android device <=2.3 Os then it play, if you are using device >4.0 then you need to give one permission in android manifest file.

      android:hardwareAccelerated="true"

      Delete
    2. Hi,
      Any body can help me? i have use above source code for live straming video. every thing is right but i want to know when i stop the video can i get size of played video which i have seen. is it possible get size of video from live streaming?? how much mb/kb video i have seen.
      if anyone know please help me. source code mail me at majid.khuhro@gmail.com

      Delete
  18. I trying to run live streaming of ODIYA TV http://www.odishatv.in/TV/live_tv.php
    but it is not working ..
    please help me on this.

    Showing Cant Play this video ..

    ReplyDelete
  19. I trying to run live streaming of ODIYA TV http://www.odishatv.in/TV/live_tv.php
    but it is not working ..
    please help me on this.

    Showing Cant Play this video ..

    ReplyDelete
  20. I want to play Live TV channel like Colors, Aaj tak, Sony, Max etc
    in my android app.
    Please can you send me the code, how to play live tv ?
    at manoj.cs.iu@gmail.com
    Plz help meas soon as possible

    ReplyDelete
  21. Hello Manoj,

    yes it possible but it depend on live streaming Url, it you can play on Video view then you can used Html 5 for playing video.

    ReplyDelete
  22. Hello I want to play video from url http://www.universitiesdata.com/?c=1 anyone help please

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. Hi,
    i want to play live tv channel like "http://livetvchannelsfree.com/aajtak.html"
    kindly request you to check how to play this url in above project.
    How to play this url in videoview .

    ReplyDelete
  25. i want to create a app like view my ip camera in the my own app pls help to create and if anyone have the code send to me prabhu2420@gmail.com please thanks.

    ReplyDelete
  26. Hi,
    Any body can help me? i have use above source code for live straming video. every thing is right but i want to know when i stop the video can i get size of played video which i have seen. is it possible get size of video from live streaming?? how much mb/kb video i have seen.
    if anyone know please help me. source code mail me at majid.khuhro@gmail.com

    ReplyDelete
  27. Hi,
    How to play live video streams from urls like this, rtmp://202.75.154.34:1935/live/livestream

    ReplyDelete
  28. Hi
    Can any one tell me the example link for live video
    http://www.yupptv.com/sakshitv.html
    its not working.

    ReplyDelete
  29. Hello Hasmukh, I would like to ask you how to develop an application for video sharing between two androids using WIFI-Direct in real time, thank you.

    ReplyDelete
    Replies
    1. Hello Ibrahim,
      Yes that you can do, there is a sample code in android sdk samplecode folder.
      androidSDK\samples\android-19\connectivity\NetworkConnect

      here there you can share file between android phone using wifi.

      Thanks

      Delete
    2. hello i read ibrahim comment and i like the idea but if i want to do the same thing as following ( Live video streaming between two androids using Peer to peer connection (wifi-direct) where one of the android is going to broadcast the video over the internet ( HTTP access )

      Delete
  30. Hi Hasmukh,
    I'm developing an app for traffic updates in a video format .In the app the user can monitor a road for how the traffic density is .we need to send traffic update as a video through cameras which monitor the live traffic and at the same time the user can view it.

    ReplyDelete
  31. This comment has been removed by the author.

    ReplyDelete
  32. Hi, This code working fine for me if i change following statment

    String videourl=""; to String videourl="http://www.pocketjourney.com/downloads/pj/video/famous.3gp";

    Thank you

    ReplyDelete
  33. hi, very useful tutorial for beginners. But I am trying to use url as http://zingo.tv/watch.php?id=36 , and its showing dialog as cant play this video. Can you please help me out. I completely followed your code, only a String variable is changed to that url.


    Thank you

    ReplyDelete
  34. Hi,

    Can anyone please tell me how to download the project/source code for this example?
    Thanks !!

    ReplyDelete
  35. how to play the axis network camera live video like(http://wmccpinetop.axiscam.net/view/view.shtml?id=153155&imagepath=/mjpg/video.mjpg) in android application any one can you help me??

    ReplyDelete
  36. Live streaming is the latest fad on social media so make sure to get live with your audience from time to

    time.
    Live streaming would give you a more intimate space with your viewers and encourage them to interact with

    you more often. It would allow more personal interaction with your audience in real time.
    You can also share your live videos later, so that those who have missed out the live streaming can view

    them again. A tour of your business, a demonstration of your products or an interview with the creators

    are some of the possible content which can be created with a live video.
    Make sure to make the most out of the apps which allows you to go live directly from the blog.

    ReplyDelete
  37. Live streaming is the latest fad on social media so make sure to get live with your audience from time to time.
    Live streaming would give you a more intimate space with your viewers and encourage them to interact with you more often. It would allow more personal interaction with your audience in real time.
    You can also share your live videos later, so that those who have missed out the live streaming can view them again. A tour of your business, a demonstration of your products or an interview with the creators are some of the possible content which can be created with a live video.
    Make sure to make the most out of the apps which allows you to go live directly from the blog.

    ReplyDelete
  38. Live streaming is the latest fad on social media so make sure to get live with your audience from time to time.
    Live streaming would give you a more intimate space with your viewers and encourage them to interact with you more often. It would allow more personal interaction with your audience in real time.
    You can also share your live videos later, so that those who have missed out the live streaming can view them again. A tour of your business, a demonstration of your products or an interview with the creators are some of the possible content which can be created with a live video.
    Make sure to make the most out of the apps which allows you to go live directly from the blog.

    ReplyDelete
  39. Live streaming is the latest fad on social media so make sure to get live with your audience from time to time.
    Live streaming would give you a more intimate space with your viewers and encourage them to interact with you more often. It would allow more personal interaction with your audience in real time.
    You can also share your live videos later, so that those who have missed out the live streaming can view them again. A tour of your business, a demonstration of your products or an interview with the creators are some of the possible content which can be created with a live video.
    Make sure to make the most out of the apps which allows you to go live directly from the blog.

    ReplyDelete
  40. Live streaming is the latest fad on social media so make sure to get live with your audience from time to time.
    Live streaming would give you a more intimate space with your viewers and encourage them to interact with you more often. It would allow more personal interaction with your audience in real time.
    You can also share your live videos later, so that those who have missed out the live streaming can view them again. A tour of your business, a demonstration of your products or an interview with the creators are some of the possible content which can be created with a live video.
    Make sure to make the most out of the apps which allows you to go live directly from the blog.

    ReplyDelete