Streaming video with videoview

旧巷老猫 提交于 2020-01-10 09:21:17

问题


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?


回答1:


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

 }
}



回答2:


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.




回答3:


You shouldn't play the video instantly. Add OnPrepared listener to the video view and start the video playing after it. With MediaPlayer you could keep track of the buffering state and stop the video for a while when its not yet downloaded. Please have a look at this guide.




回答4:


Try Intent to avoid that error. or put try catch in your block. VideoView can only Stream 3gp videos but i recommend this code to stream your video

public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
String videourl = "http://something.com/blah.mp4";
Uri uri = Uri.parse(videourl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
}

Or Click here to watch Android Video Streaming Tutorial.



来源:https://stackoverflow.com/questions/9765989/streaming-video-with-videoview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!