Video Playback Demo

Source: Internet
Author: User

Words do not say much, directly on the code: XML file:

<TextView Android:id= "@+id/file"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "File name"/> <EditText Android:id= "@+id/fileedite"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout> <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <Button Android:id= "@+id/play"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Mediaplay"/> <Button Android:id= "@+id/pause"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Mediaplay"/> <Button Android:id= "@+id/reset"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Mediaplay"/> <Button Android:id= "@+id/stop"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "Mediaplay" > </Button> </LinearLayout> <Surfaceview android:layout_width= "Fill_parent"Android:layout_height= "240DP"Android:id= "@+id/surfaceview"/>
View Code


    PrivateEditText Nameedit; PrivateMediaPlayer Media; PrivateString Path; PrivateSurfaceview Surfaceview; PrivateBoolean pause=false; Private intposition; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_media_play); Media=NewMediaPlayer (); Nameedit=(EditText) Findviewbyid (; //send the video footage to the Surfaceview, directly to the screen, not in its own buffersurfaceview=(Surfaceview) Findviewbyid (;        Surfaceview.getholder (). SetType (Surfaceholder.surface_type_push_buffers); Surfaceview.getholder (). Setfixedsize (176, 144); Surfaceview.getholder (). Setkeepscreenon (true); Surfaceview.getholder (). Addcallback (Newsurfacecallback ()); }         Public Final classSurfacecallbackImplementscallback{ Public voidsurfacecreated (Surfaceholder holder) {//TODO auto-generated Method Stub            if(Position>0 && path!=NULL) {play (position); Position=0; }        }                 Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) {            //TODO auto-generated Method Stub                    }                 Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method Stub            if(Media.isplaying ()) {position=media.getcurrentposition ();            Media.stop (); }                    }        }    Private voidMediaplay (View v) {Switch(V.getid ()) { filename=Nameedit.gettext (). toString (); if(Filename.startswith ("http") ) {path=filename; Play (0); }Else{File File=NewFile (environment.getexternalstoragedirectory (), filename); if(File.exists ()) {path=File.getabsolutepath (); Play (0); }Else{Path=NULL; Toast.maketext ( This, "Video file does not exist", 1). Show (); }            }                         Break; ()) {media.pause (); Pause=true; }Else{                if(pause) {Media.start (); Pause=false; }            }             Break; ()) {Media.seekto (0); }Else{                if(path!=NULL) {Play (0); }            }             Break; ()) {media.stop (); }             Break; default:                 Break; }} @Overrideprotected voidOnDestroy () {//TODO auto-generated Method Stubmedia.release (); Media=NULL; Super. OnDestroy (); }    Private voidPlayintposition)        {Media.reset (); Try{media.setdatasource (path);            Media.setdisplay (Surfaceview.getholder ()); Media.prepare ();//BufferingMedia.setonpreparedlistener (NewPreparedlistener (position)); } Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SecurityException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }        Private Final classPreparedlistenerImplementsonpreparedlistener{Private intposition;  PublicPreparedlistener (intposition) {             This. position=position; } @Override Public voidonprepared (MediaPlayer MP) {//TODO auto-generated Method StubMedia.start (); if(Position>0 && path!=NULL) {media.seekto (position); }        }        
View Code

Video Playback Demo

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: 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.