Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play online videos in Android?

I am developing a sample media player app in Android to play online videos, I have developed some code to play videos. It plays video from SD card very well, but I am facing two issues in this respect.

1- when I open the app for online videos it shows the video but it does not play the video and after a while it displays an alert dialog having message "Sorry, this video cannot be played".

2- I have code for "Progress bar", but I don't know where to put the code when the video is downloading from the internet.

Here is my code you can check it. Thanks in advance.

myVideoView = (VideoView) findViewById(R.id.surface_view);


        try
        {

            myVideoView.setVideoURI(Uri.parse("http://www.MY_DOMAIN_NAME.com/videos/video1.mp4"));
            myVideoView.setMediaController(new MediaController(this));
            myVideoView.requestFocus();
            myVideoView.start();
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(), "No Media found", Toast.LENGTH_LONG).show();
        }
like image 406
user1703737 Avatar asked Feb 11 '13 10:02

user1703737


2 Answers

try like this

 VideoView v;
 MediaController mediaController;
 ProgressDialog progressDialog;

then

public void playvideo(String videopath) {
    Log.e("entered", "playvide");
    Log.e("path is", "" + videopath);
    try {
        progressDialog = ProgressDialog.show(VideoPlay.this, "",
                "Buffering video...", false);
        progressDialog.setCancelable(true);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        mediaController = new MediaController(VideoPlay.this);

        Uri video = Uri.parse(videopath);
        v.setMediaController(mediaController);
        v.setVideoURI(video);

        v.setOnPreparedListener(new OnPreparedListener() {

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

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

}

if still has problem "this video cannot be played" try to change video format , hope it will help u.

like image 159
Senthil Avatar answered Oct 15 '22 13:10

Senthil


It is very easy to play Live video using Video view in Android. i am pasting code here, try this one it will work fine for you.

public class PlayVideo extends Activity

{

      //private String videoPath ="Url";

      private static ProgressDialog progressDialog;
      String videourl; 
      VideoView videoView ; 

      protected void onCreate(Bundle savedInstanceState)
      {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.play_video);

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

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


            PlayVideo();

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

                  Uri video = Uri.parse(videourl);           
                  videoView.setMediaController(mediaController);
                  videoView.setVideoURI(videoPath);
                  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();
            } 

      }
}

You can find code from below link:

http://hasmukhbhadani.blogspot.in/search/label/Video-Play%20Live%20Streaming%20in%20Android.

like image 42
Hasmukh Avatar answered Oct 15 '22 12:10

Hasmukh