Android 98 Mediaplayer+surfaceview Play video

Source: Internet
Author: User

Package Com.itheima.videoplayer;import Java.io.ioexception;import android.media.mediaplayer;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.SurfaceHolder;import Android.view.surfaceholder.callback;import Android.view.SurfaceView; Public classMainactivity extends Activity {PrivateMediaPlayer player;//can only play MP4 and 3gp format video    Static intcurrentposition; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Surfaceview tags must be displayed on the page before they can be used, so another thread will be opened to sleep for a while and then the layout is generated to use the Surfaceview object. Surfaceview SV = (surfaceview) Findviewbyid (R.ID.SV);//Video Playback Screen//get Surfaceview's controller.Final Surfaceholder sh =Sv.getholder ();//thread t = new Thread () {//@Override//Public void Run () {//try {//sleep (200); //200 milliseconds can't write dead//} catch (Interruptedexception e) {//e.printstacktrace ();//                }//Runonuithread (New Runnable () {//in the main thread to execute//@Override//Public void Run () {//MediaPlayer player = new MediaPlayer ();//Player.reset ();//try {//Player.setdatasource ("sdcard/2.3gp");//player.setdisplay (SH);//Player.prepare ();//Player.start ();//} catch (Exception e) {//e.printstacktrace ();//                        } //                    }//                });//            }//        };//T.start ();Sh.addcallback (NewCallback () {//called when Surfaceview is destroyed@Override Public voidsurfacedestroyed (Surfaceholder holder) {//stop playing the video every time Surfaceview is destroyed                if(Player! =NULL) {currentposition=player.getcurrentposition ();                    Player.stop ();                    Player.release (); Player=NULL; }            }            //Surfaceview Called when Surfaceview is visible when it is created@Override Public voidsurfacecreated (Surfaceholder holder) {//play video Every time Surfaceview is created                if(Player = =NULL) {player=NewMediaPlayer ();                    Player.reset (); Try{Player.setdatasource ("sdcard/2.3gp");                        Player.setdisplay (SH);                        Player.prepare ();                        Player.start ();                    Player.seekto (currentposition); } Catch(Exception e) {e.printstacktrace (); }                 }            }            //called when the Surfaceview structure changes@Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight)    {            }        }); }}/*page: <surfaceview android:id= "@+id/sv" android:layout_width= "match_parent" android:layout_height= "Match_pare NT "/>---#视频播放器 # #SurfaceView * Real-time update of the screen requires high * Dual buffering technology: There are two canvases in memory, a canvas is displayed to the screen, the B canvas draws the next frame in memory, B is displayed to the screen after drawing, and a continues to draw the next frame in memory * Play video is also used MediaPlayer, but unlike audio, to set the display in which Surfaceview*/

Vitamio Play Video:

Package Com.itheima.videoview;import Io.vov.vitamio.libschecker;import Io.vov.vitamio.widget.MediaController; Import Io.vov.vitamio.widget.videoview;import Android.os.bundle;import Android.app.activity;import Android.view.Menu; Public classMainactivity extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //detects if Vitamio is supported, installs without installation        if(! Libschecker.checkvitamiolibs ( This)) {return;} Videoview VV=(Videoview) Findviewbyid (R.ID.VV); Vv.setvideopath ("SDCARD/4.RMVB");        Vv.start (); Vv.setmediacontroller (NewMediacontroller ( This));//Video Playback Controller    }}/*# # #SurfaceView * Double buffering technology * heavy-weight components (more resource-intensive) * As long as not visible, will not be created, visible, will be created * as long as not visible, will destroy # # # ffmpeg* Open source free audio and video codec, in C language development. # # # vitamio* encapsulates the FFmpeg video playback framework * All APIs provided are JAVAAPI*//*<io.vov.vitamio.widget.videoview android:id= "@+id/vv" android:layout_width= "Match_parent" Android:layout_h eight= "Match_parent"/>*///manifest file: <activity android:name= "io.vov.vitamio.activity.InitActivity" ></activity>

Android 98 Mediaplayer+surfaceview Play 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.