Streaming video with videoview

后端 未结 4 915
甜味超标
甜味超标 2020-12-29 16:50

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


        
相关标签:
4条回答
  • 2020-12-29 17:06

    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();
           }   
    
     }
    }
    
    0 讨论(0)
  • 2020-12-29 17:11

    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.

    0 讨论(0)
  • 2020-12-29 17:14

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题