Cheng Xiang Ink Film
android--use Videoview to play video foreword have previously talked about how to use Surfaceview with MediaPlayer to play video, in fact, Android also provides developers with another more simple broadcast The way to put the video media, that is Videoview, this blog is about Videoview how to play the video, the final will be a simple demo demo. The main content of this blog is as follows: videoviewvideoview simple Demomediacontrollermediacontroller Simple Demo videoview Videoview, used to play a video media, which inherits the Surfaceview, is located in "Android.widget.VideoView" and is a video control. Since it is playing a video, then inevitably involves some start, pause, stop and other operations, Videoview also for developers to provide a corresponding method, here is a brief introduction of some commonly used: int getcurrentposition () : Gets the current playback position. int getduration (): Gets the total length of the currently playing video. IsPlaying (): Whether the current videoview is playing video. void Pause (): pauses void seekto (int msec): plays from the first few milliseconds. void Resume (): Replay. void Setvideopath (String path): Sets the Videoview playback video source as a file path. void Setvideouri (URI uri): Sets the Videoview playback video source as a URI, either a network URI or a local URI. void Start (): Starts playback. void Stopplayback (): Stops playback. Setmediacontroller (Mediacontroller Controller): Set up Mediacontroller controllers. Setoncompletionlistener (Mediaplayer.oncompletionlistener L): listens for events that have finished playing. Setonerrorlistener (Mediaplayer.onerrorlistener L): Listens for events when an error occurs. Setonpreparedlistener (MedIaplayer.onpreparedlistener l):: Monitor the event that the video load completes. Some of the above methods can be used to understand the purpose of the method name. Unlike MediaPlayer with Surfaceview playback video, Videoview does not need to encode the video before playing, it automatically loads the video when start () starts playing. And Videoview does not need to encode the recycle resource after it is exhausted. videoview Simple demo Videoview In fact there is no difficulty, through its own API method, you can complete a video playback, nothing more than pay attention to the timing of the call method. The following is a simple demo showing how Videoview plays a video file on an SD card. Four buttons are provided in the demo to play, pause, replay, stop, and display with the progress bar, respectively. The code comments are complete and the details are no longer described here. Layout codes: Activity_videoview.xml view Code implementation Codes:videoviewactivity.java copy code 1 package cn.bgxt.videoviewdemo; 2 3 Import java.io.file; 4 5 import android.app.activity;& nbsp 6 Import android.media.mediaplayer; 7 import android.media.mediaplayer.oncompletionlistener; 8 Import android.media.mediaplayer.onerrorlistener; 9 Import android.os.bundle; 10 Import android.util.log; Import android.view.view; 12 import android.widget.button; 13 import android.widget.edittext; 14 Import android.widget.seekbar; 15 ImpoRT android.widget.toast; 16 Import android.widget.videoview; 17 Import ANDROID.WIDGET.SEEKBAR.ONSEEKBARCHANGELISTENER; 18  19 public class Videoviewactivity extends Activity { 20 private final String TAG = "main"; 21 Private EditText et_path; 2 2 Private Button Btn_play, Btn_pause, Btn_replay, btn_stop; 23 private SeekBar SEEKBAR;&N bsp;24 Private Videoview vv_video; 25 Private Boolean isplaying; 26 27 & nbsp @Override 28 protected void onCreate (Bundle savedinstancestate) { 29 & nbsp TODO auto-generated method stub 30 super.oncreate (savedinstancestate); 31 Setcontentview (R.layout.activity_videoview), 32 33 & nbsp SeekBar = (SeekBar) Findviewbyid (R.id.seekbar); 34 &NBSp Et_path = (EditText) Findviewbyid (R.id.et_path), 35 Vv_video = (videoview) Findvie Wbyid (R.id.vv_videoview), 36 37 Btn_play = (Button) Findviewbyid (r.id.btn_ Play), 38 Btn_pause = (Button) Findviewbyid (r.id.btn_pause); 39 &NBSP ; Btn_replay = (button) Findviewbyid (r.id.btn_replay) 40 Btn_stop = (Button) Findvi Ewbyid (r.id.btn_stop), 41 42 Btn_play.setonclicklistener (click); Btn_pause.setonclicklistener (click), 44 Btn_ Replay.setonclicklistener (click), 45 Btn_stop.setonclicklistener (click); 46 47 //Add progress change events to the progress bar 48 Seekbar.setonseekbarchange Listener (change); 49 } 50 51 Private Onseekbarchangelistener change = new Onseekbarchangelistener () { 52 53 & nbsp @Override 54 public void Onstoptrackingtouch (SeekBar SeekBar) {  //Trigger 56 //To get current progress when the progress bar stops modifying Tick 57 int progress = seekbar.getprogress () 58 if (vv_video! = null && vv_video.isplaying ()) { 59 &N Bsp /Set current playing position 60 Vv_video.seekto (progress)   } 62 } 63 64 @Override 65 public void Onstarttrackingtouch (SeekBar SeekBar) { 66&N bsp; 67 &nbsP } 68 69 @Override 70 Pub LIC void onprogresschanged (SeekBar SeekBar, int progress, 71 Boolean Fromuser) { 72 73 } 74 }; 75 PRI vate View.onclicklistener click = New View.onclicklistener () { 76 77 @Overrid e 78 public void OnClick (View v) { 79 80 &NBS P Switch (V.getid ()) { 81 case r.id.btn_play: 82 &N Bsp Play (0) 83 break; 84 case r.id.btn_pause: 85 and nbsp Pause (); 86 break; 87 Case r.id.btn_replay: 88 Replay () 89 &NBS P break; 90 Case r.id.btn_stop: 91 &N Bsp Stop () 92 B reak; 93 default: 94 break; 95 } 96 } 97 &NB Sp }; 98 99 protected void play (int msec) {$ LOG.I (TAG, "Get video file to 101 String path = Et_path.gettext (). toString (). Trim (), 102 File FILE = newFile (Path), 103 if (!file.exists ()) {104 Toast.makete XT (This, "Video file path error", 0). Show (); return;106 }107 & nbsp 108 LOG.I (TAG, "Specify video Source Path"), 109 VV_VIDEO.S Etvideopath (File.getabsolutepath ()), LOG.I (TAG, "start playing"), 111 VV _video.start (), 113 //play in the initial position of the &N Bsp Vv_video.seekto (msec); //Set the maximum progress of the progress bar to the maximum playback duration of the video stream (max. Seekbar.set Max (Vv_video.getduration ()); 117 118 //start thread, update progress bar scale 119 New T Hread () {120 121 @Override122 Pub LIC void Run (){123 try {124 &NB Sp isplaying = true;125 while (isplaying) {126 //if playing, no 0.5. Milliseconds Update progress bar 127 &nbs P int current = Vv_video.getcurrentposition (); 1 seekbar.setprogress (current); 129&NB sp;130 sleep ($), 131 &N Bsp }132 } catch (Exc Eption e) {133 E.printstacktrace () 134 &NBSP }135 }136 }.start (); 137 //Play after playback button is not available 138 btn_play.setenabled (false); 139 140   ; Vv_video.setoncompletionlistener (new Oncompletionlistener () {141 142 & nbsp @Override143 public void Oncompletion (MediaPlayer MP) {144 //After play has been callback 145 BT N_play.setenabled (True), 146 }147 }); 148 149 &nb Sp Vv_video.setonerrorlistener (new Onerrorlistener () {150 151 & nbsp @Override152 public Boolean onError (MediaPlayer MP, int. what, int extra) {153 &N Bsp //Error Replay 154 Play (0) 1 isplaying = false;156 return false;157 }158 }); 159 & nbsp }160 161 /**162 * re-start playback 163 */164 protected void Replay () {165 if (vv_video! = null && vv_video.isplaying ()) {166 Vv_video.seekto (0), 167 Toast.maketext (this, "replay", 0). Show () 168 Btn_pause.settext ("pause"), 169 RET urn;170 }171 isplaying = false;172 PLA Y (0); 173 174 }175 176 /**177 * pause or Continue 178 */179 protected void Pause () { if (Btn_pause.gettext (). toString (). Trim (). Equals ("continue")) {181 &nbs P Btn_pause.settext ("pause"), 182 Vv_video.start (); 183 Toast.maketext (this, "continue play", 0). Show (); 184 &NBSP ; return;185 }186 if (vv_video! = null && vv_video.is Playing ()) {187 vv_video.pause (); 188 B Tn_pause.settext ("Continue"), 189 Toast.maketext (this, "pause playback", 0). Show (); Bsp }191 }192 193 /*194 * stop play 195  */196 Protected void Stop () {197 if (vv_video! = null && vv_video.isplaying ()) {198 &N Bsp Vv_video.stopplayback () 199 btn_play.setenabled (tru e) isplaying = false;201 }202 }203 Copy code effect show: mediacontroller mentioned Videoview had to say some mediacontroller. Although Videoview provides us with a handy API for playing, pausing, stopping, and so on, we still need to do the coding, but if we use Mediacontroller, then these operations can be omitted. Mediacontroller can be used to play a video with Videoview, it provides videoview a suspended action bar, in the action bar can control the video playback Videoview, by default, will hover for three seconds. It uses the Mediacontroller.setmediaplayer () method to specify the Videoview to be controlled, but this is not enough, mediacontroller control needs to be similar to two-way control, Mediacontroller specifies that the control Videoview,videoview also need to specify that Mediacontroller to control it, which requires the use of the Videoview.setmediacontroller () method. The following describes some common methods of Mediacontroller; boolean isshowing (): Whether the current hover control bar is displayed. void Setmediaplayer (Mediacontroller.mediaplayercontrol plAyer): Sets the components of the control. void Setprevnextlisteners (View.onclicklistener next,view.onclicklistener prev): Sets the toggle event for the previous video and the next video. The above method can be seen Setmediaplayer () is not specified is a videoview, but a MediaPlayerControl interface, the MediaPlayerControl interface defines some play-related play, pause, Stop, and Videoview implements the MediaPlayerControl. By default, Mediacontroller does not display these two buttons if the listener for the video is not set through Setprevnextlisteners (). mediacontroller simple demo above has been said Mediacontroller some of the content, Below is a simple demo to demonstrate Mediacontroller control Videoview play video. Layout codes: Activity_controller.xml view Code implementation Codes:controlleractivity.java copy code 1 package Cn.bgxt.videoviewdemo; 2 3 import java.io.file; 4 5 Import android.app.Activity; 6 Import android.os.bundle; 7 import android.view.view; 8 import Android.view.View.OnClickListener; 9 Import android.widget.mediacontroller;10 import android.widget.toast;11 import android.widget.videoview;12  13 public class Controlleractivity extends Activity { PrivaTe Videoview vv_video;15 Private Mediacontroller mcontroller;16 17 @Overrid E18 protected void onCreate (Bundle savedinstancestate) { Super.oncreate (Savedi Nstancestate) Setcontentview (R.layout.activity_controller), vv_video= (Videoview) Findviewbyid (R.id.vv_video), //instantiation MediaController23 & nbsp Mcontroller=new Mediacontroller (this), file File=new file ("/sdcard/ Ykzzldx.mp4 ") if (file.exists ()) {2 //Set the path for the video source to play. 7 Vv_video.setvideopath (File.getabsolutepath ()), & nbsp //For Videoview specify MediaController29 Vv_video.setmediacontroller ( Mcontroller) &NBsp //Mediacontroller specified control VideoView31 Mcontroller.setmediaplayer (vv_ Video), //increase monitoring of the previous and next toggle events, which are not displayed by default for the two buttons: Mcontroller.setprevnextlisteners (new Onclicklistener () {The 35 @Override36 &NB Sp public void OnClick (View v) { 37 Toast.maketext (Controlleractivity.this, "next", 0). Show ();   ; }39 }, new Onclicklistener () {4 0 41 @Override42 &NBSp public void OnClick (View v) {$ Toast.maketext (controlleractivity.this, "prev", 0). Show (); &NBSP ; }45 }); }47 }48} copy Code & nbsp; from the effect shown above can be seen, mediacontroller not only for us to increase the control bar to control playback, pause, fast forward, rewind, switch on a video, switch to the next video, A progress bar display is also added. http://www.2cto.com/kf/201312/261877.html
android--playing video using Videoview