Explain in detail the method of using VideoView to realize video playing in Android App

  • 2021-07-03 00:50:49
  • OfStack

To play video through VideoView:
1. Define the VideoView component in the interface layout file, or create the VideoView component in the program
2. Call the following two methods of VideoView to load the specified video
(1) setVidePath (String path): Loads the video represented by the path file
(2) setVideoURI (Uri uri): Load the video corresponding to uri
3. Call start (), stop () and psuse () methods of VideoView to control the playing of video

By using VideoView in conjunction with the MediaController class, developers do not have to control playback and pause by themselves


package cn.com.chenzheng_java; 
 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.MediaController; 
import android.widget.VideoView; 
import android.widget.MediaController.MediaPlayerControl; 
 
public class VideoActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.video); 
     
    VideoView videoView = (VideoView)findViewById(R.id.videoView1); 
    /*** 
     *  Associate a player with 1 Audio or video files  
     * videoView.setVideoURI(Uri uri) 
     * videoView.setVideoPath(String path) 
     *  Both of the above methods can be used.  
     */ 
    videoView.setVideoPath("data/yueding.mp3"); 
     
    /** 
     * w Provide it with 1 Controller, control its pause, play … and other functions  
     */ 
    videoView.setMediaController(new MediaController(this)); 
     
    /** 
     *  Method of triggering video or audio to the end  
     */ 
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
        Log.i(" Notice ", " Finish "); 
      } 
    }); 
     
    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { 
       
      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
        Log.i(" Notice ", " Error in playback "); 
        return false; 
      } 
    }); 
     
  } 
} 

video.xml


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" android:layout_height="match_parent" 
  android:orientation="horizontal"> 
  <VideoView android:layout_height="match_parent" android:id="@+id/videoView1" 
    android:layout_width="wrap_content"></VideoView> 
</LinearLayout> 

Of course, we can also play multimedia on the network.


Related articles: