Play local video

Source: Internet
Author: User

1. Play local video

Playlocationvideoinsdactivity.java

Key point 1:

String Path = "/sdcard/tzh.mp4";
String path2 = "" +environment.getexternalstoragedirectory () + "/tzh.mp4";//equivalent above address

Key point 2: Control progress bar

Package Com.example.videoplaydemo;import Android.app.activity;import Android.os.bundle;import Android.os.environment;import Java.io.file;import Android.app.activity;import Android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.mediacontroller;import Android.widget.toast;import Android.widget.videoview;import Java.io.file;import Android.app.Activity;import Android.media.mediaplayer;import Android.media.mediaplayer.oncompletionlistener;import Android.media.mediaplayer.onerrorlistener;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.SeekBar; Import Android.widget.toast;import Android.widget.videoview;import Android.widget.SeekBar.OnSeekBarChangeListener ;p ublic class Playlocationvideoinsdactivity extends Activity {private final String TAG = "main";p rivate EditText et_path;p Rivate Button Btn_play, Btn_pause, Btn_replay, btn_stop;private SeekBar seekbar;private videoview vv_video;private boolean isplaying; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.acticity_play_sd_video); SeekBar = (SeekBar) Findviewbyid (r.id.seekbar); Et_path = (EditText) Findviewbyid ( R.id.et_path); vv_video = (Videoview) Findviewbyid (r.id.vv_videoview); btn_play = (Button) Findviewbyid (R.id.btn_play) ; btn_pause = (Button) Findviewbyid (r.id.btn_pause); btn_replay = (Button) Findviewbyid (r.id.btn_replay); btn_stop = ( Button) Findviewbyid (r.id.btn_stop); Btn_play.setonclicklistener (click); Btn_pause.setonclicklistener (click); btn_ Replay.setonclicklistener (click); Btn_stop.setonclicklistener (click);// Add progress Change event Seekbar.setonseekbarchangelistener for progress bar;} Private Onseekbarchangelistener change = new Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch ( SeekBar SeekBar) {//When the progress bar stops modifying, the tick int progress = seekbar.getprogress () of the current progress bar is triggered///); if (vV_video! = null && vv_video.isplaying ()) {//Set the current playing position vv_video.seekto (progress);}} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {} @Overridepublic void onprogresschanged (SeekBar SeekBar, int Progress,boolean fromuser) {}};p rivate View.onclicklistener click = New View.onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_play:play (0); break;case r.id.btn_pause:pause (); Break;case r.id.btn _replay:replay (); break;case r.id.btn_stop:stop (); break;default:break;}}; protected void Play (int msec) {log.i (TAG, "Get video file Address"); String Path = "/sdcard/tzh.mp4";//string path2 = "" +environment.getexternalstoragedirectory () + "/tzh.mp4";// Equivalent to the above address file File = new file (path), if (!file.exists ()) {Toast.maketext (this, "Video file path error", 0). Show (); return;} LOG.I (TAG, "Specify video source Path"); Vv_video.setvideopath (File.getabsolutepath ()); LOG.I (TAG, "start playing"), Vv_video.start ();//Play Vv_video.seekto (msec) in the initial position,//Set the maximum progress of the progress bar to the maximum playback duration of the video stream Seekbar.setmax (vv_ Video.getduration ());//start thread, update progress barScale new Thread () {@Overridepublic void run () {try {isplaying = True;while (isplaying) {////if playing, no 0.5. Milliseconds Update progress bar int current = Vv_video.getcurrentposition (); seekbar.setprogress (current); sleep (500);}} catch (Exception e) {e.printstacktrace ();}}}. Start ();//play after the Set play button is not available btn_play.setenabled (false); Vv_video.setoncompletionlistener (new Oncompletionlistener () {@ overridepublic void Oncompletion (MediaPlayer MP) {//callback btn_play.setenabled (TRUE) after playback;}); Vv_video.setonerrorlistener (New Onerrorlistener () {@Overridepublic Boolean onError (MediaPlayer MP, int what, int extra) {//Error replay play (0); isplaying = False;return false;}});} /** * * Re-start playback */protected void Replay () {if (Vv_video! = null && vv_video.isplaying ()) {vv_video.seekto (0); Toast.maketext (This, "replay", 0). Show (); Btn_pause.settext ("pause"); return;} isplaying = False;play (0);} /** * * Pause or resume */protected void pause () {if (Btn_pause.gettext (). toString (). Trim (). Equals ("continue")) {Btn_pause.settext ("pause" ); Vv_video.start (); Toast.maketext (This, "continue broadcastingPut ", 0). Show (); return;} if (Vv_video! = null && vv_video.isplaying ()) {vv_video.pause (); Btn_pause.settext ("Continue"); Toast.maketext (This, "pause playback", 0). Show ();}} /* * Stop playing */protected void Stop () {if (Vv_video! = null && vv_video.isplaying ()) {vv_video.stopplayback (); btn_p Lay.setenabled (true); isplaying = false;}}}

XML file:

<?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=" vertical " > <edittext android:id= "@+id/et_path" android:layout_width= "Match_parent" android:layout         _height= "Wrap_content" android:text= "/sdcard/ykzzldx.mp4"/> <seekbar android:id= "@+id/seekBar"        Android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <linearlayout  Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <button android:id= "@+id/btn_play" android:layout_width= "0dip" Androi d:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Play"/> <butt On android:id= "@+id/btn_Pause "android:layout_width=" 0dip "android:layout_height=" Wrap_content "Android:layout_we ight= "1" android:text= "pause"/> <button android:id= "@+id/btn_replay" Andro Id:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" and            roid:text= "Replay"/> <button android:id= "@+id/btn_stop" android:layout_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Stop"/> &        lt;/linearlayout> <videoview android:id= "@+id/vv_videoview" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent"/> </LinearLayout>

Play local video

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.