Objective
Continue to toss VLC, do this example is not smooth, stuck in only voice no image this problem for a long time, online example some API has been on, continue to share,,,
Statement
Welcome reprint, but please keep the original source of the article:)
Blog Park: http://www.cnblogs.com
Farmer Uncle: Http://over140.cnblogs.com
Body
Project
Code
PublicClass VlcvideoactivityExtends ActivityImplementsSurfaceholder.callback, Ivideoplayer {PrivateFinalstatic String TAG = "[Vlcvideoactivity]";PrivateSurfaceview Msurfaceview;PrivateLIBVLC Mmediaplayer;PrivateSurfaceholder Msurfaceholder;PrivateView Mloadingview;PrivateIntMvideoheight;PrivateIntMvideowidth;PrivateIntMvideovisibleheight;PrivateIntMvideovisiblewidth;PrivateIntMsarnum;PrivateIntMsarden; @OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_VIDEO_VLC); Msurfaceview =(Surfaceview) Findviewbyid (R.id.video); Mloadingview =Findviewbyid (r.id.video_loading);Try{Mmediaplayer =Vlcinstance.getlibvlcinstance (); }Catch(Libvlcexception e) {E.printstacktrace ();} Msurfaceholder =Msurfaceview.getholder (); Msurfaceholder.setformat (pixelformat.rgbx_8888); Msurfaceholder.addcallback (This); Mmediaplayer.eventvideoplayeractivitycreated (True); EventHandler em =Eventhandler.getinstance (); Em.addhandler (Mvlchandler);This. Setvolumecontrolstream (Audiomanager.stream_music); Msurfaceview.setkeepscreenon (True);//Mmediaplayer.setmedialist ();//Mmediaplayer.getmedialist (). Add (New Media (Mmediaplayer, "Http://live.3gv.ifeng.com/zixun.m3u8"), false);//Mmediaplayer.playindex (0); MMEDIAPLAYER.PLAYMRL ("http://live.3gv.ifeng.com/zixun.m3u8"); } @OverridePublicvoidOnPause () {Super. OnPause ();if (mmediaplayer! =Null) {mmediaplayer.stop (); Msurfaceview.setkeepscreenon (False); }} @OverrideProtectedvoidOnDestroy () {Super. OnDestroy ();if (mmediaplayer! =Null) {mmediaplayer.eventvideoplayeractivitycreated (False); EventHandler em =Eventhandler.getinstance (); Em.removehandler (Mvlchandler); }} @OverridePublicvoidOnconfigurationchanged (Configuration newconfig) {setsurfacesize (Mvideowidth, Mvideoheight, Mvideovisiblewidth, Mvideovisibleheight, Msarnum, Msarden);Super. onconfigurationchanged (Newconfig); } @OverridePublicvoidSurfacecreated (Surfaceholder holder) {if (mmediaplayer! =Null) {Msurfaceholder =Holder Mmediaplayer.attachsurface (Holder.getsurface (),This); }} @OverridePublicvoid Surfacechanged (Surfaceholder holder,int format,int width,IntHeight) {Msurfaceholder =Holderif (mmediaplayer! =Null) {Mmediaplayer.attachsurface (Holder.getsurface (),this);//, Width, height}if (Width > 0) {Mvideoheight =Height Mvideowidth =Width }} @OverridePublicvoidSurfacedestroyed (Surfaceholder holder) {if (mmediaplayer! =Null) {mmediaplayer.detachsurface ();}} @OverridePublicvoid Setsurfacesize (int width,int height,int Visible_width,int Visible_height,int Sar_num,IntSar_den) {mvideoheight =Height Mvideowidth =Width Mvideovisibleheight =Visible_height; Mvideovisiblewidth =Visible_width; Msarnum =Sar_num; Msarden =Sar_den; Mhandler.removemessages (handler_surface_size); Mhandler.sendemptymessage (handler_surface_size); }PrivateStaticFinalint Handler_buffer_start = 1;PrivateStaticFinalint handler_buffer_end = 2;PrivateStaticFinalint handler_surface_size = 3;PrivateStaticFinalint surface_best_fit = 0;PrivateStaticFinalint surface_fit_horizontal = 1;PrivateStaticFinalint surface_fit_vertical = 2;PrivateStaticFinalint Surface_fill = 3;PrivateStaticFinalint surface_16_9 = 4;PrivateStaticFinalint surface_4_3 = 5;PrivateStaticFinalint surface_original = 6;Privateint mcurrentsize =Surface_best_fit;Private Handler Mvlchandler =NewHandler () {@OverridePublicvoid Handlemessage (Message msg) {if (msg = = NULL | | msg.getdata () = = null) return; switch (Msg.getdata () getInt ("event")) {Case EventHandler.MediaPlayerTimeChanged:break, case EventHandler.MediaPlayerPositionChanged:break; EventHandler.MediaPlayerPlaying:mHandler.removeMessages (Handler_buffer_end); Mhandler.sendemptymessage (Handler_buffer_end); Break Case EventHandler.MediaPlayerBuffering:break; Case EventHandler.MediaPlayerLengthChanged:break; Case eventhandler.mediaplayerendreached://play complete break; } } }; Private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {switch (msg.what) {case HAN Dler_buffer_start:showloading (); Break Case Handler_buffer_end:hideloading (); Break Case Handler_surface_size:changesurfacesize (); Break } } }; private void Showloading () {mloadingview.setvisibility (view.visible);} private void Hideloading () { Mloadingview.setvisibility (View.gone); } private void Changesurfacesize () {//Get screen size int DW= Getwindowmanager (). Getdefaultdisplay (). GetWidth (); int dh = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); Calculate aspect ratio double AR = (double) mvideowidth/(double) mvideoheight; Calculate display aspect ratio double Dar = (double) DW/(double) DH; Switch (mcurrentsize) {case surface_best_fit:if (Dar < ar) DH = (int) (DW/AR), Else DW = (int) (DH * ar); break; CA Se surface_fit_horizontal:dh = (int) (DW/AR); Break Case SURFACE_FIT_VERTICAL:DW = (int) (DH * ar); Break Case Surface_fill:break; Case Surface_16_9:ar = 16.0/9.0; if (Dar < ar) DH = (int) (DW/AR); else DW = (int) (DH * ar); Break Case Surface_4_3:ar = 4.0/3.0; if (Dar < ar) DH = (int) (DW/AR); else DW = (int) (DH * ar); Break Case SURFACE_ORIGINAL:DH = mvideoheight; DW = Mvideowidth; Break } msurfaceholder.setfixedsize (Mvideowidth, mvideoheight); Viewgroup.layoutparams LP = Msurfaceview.getlayoutparams (); Lp.width = DW; Lp.height = DH; MSURFACEVIEW.SETLAYOUTPARAMS (LP); MsUrfaceview.invalidate (); }}
Code description
1, the vlc-android version is 2.2.0-pre1 Weatherwax
2, copy vlc-android official website example below ORG.VIDEOLAN.LIBVLC all classes, as well as Vlcinstance, Preferences. Only the voice does not have the picture on vlcinstance this question, has less some configuration, the reference vlcinstance.updatelibvlcsettings.
3, PLAYMRL do not use the URI to turn, otherwise can not open
4, the code provided has been compiled so, also modify the source open eventhandler.mediaplayerbuffering (also modified LIBVLCJNI.C), but there is no buffer progress value, still need to explore further.
5, the actual discovery if only in surfacechanged Attachsurface,setsurfacesize callback does not trigger, you need to set in surfacecreated.
6, this article does not discuss the handler non-static may cause the memory leaks the question, everybody writes the time oneself notice:)
Download
Https://github.com/over140/vlc-android-demo (note Engineering is the project of Android Studio)
Article
1, vlc-android call with libvlcjni.so to achieve streaming media playback
2, [Github]vlc-android-macbuild
3, VLC Android compilation and, recording video and other functions
4, Vlc-android source code compilation process record
5, Vlc-android get the response of mediaplayerencounterederror,mediaplayerbuffering and other events
6, Vlc-for-android Source analysis
End
Article 2 of the GitHub project is for VLC encapsulation, also quite good, worth reference. Vlc-android's example is more complex, and the API may be old, and when you look at the article, take note of the version issue.
Simple example of "vlc-android" vlc-android