Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming video with videoview

My code below to streaming video:

VideoView vv = (VideoView)this.findViewById(R.id.screen_video);
Uri uri = Uri.parse(URL);
vv.setVideoURI(uri);
vv.start();

It works. But if the URL video's format not support by android phone or pad. It show a dialog, and not show the screen. But it still streaming with black screen. I want to get the error message, and access as an exception. But I don't know how to get it?

Another problem is that the streaming may crash cause by low speed wifi. How to check it to wait while low speed wifi?

like image 296
brian Avatar asked Dec 05 '22 17:12

brian


2 Answers

try this code,it work,

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(videoPath );             
               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();
       }   

 }
}
like image 99
Hasmukh Avatar answered Dec 26 '22 01:12

Hasmukh


It depends on which Android Version you are developing your application on. There are certain devices which do not support running .mp4 file. Go through Android Media support for more information. Check if you can play any .3gp files or not.

like image 31
Rashmi.B Avatar answered Dec 26 '22 01:12

Rashmi.B