Android video playback and toggle screen

Source: Internet
Author: User

Recently did a project, which used the video play this piece, then want to consider Vitamio,demo also made out, but later found that it is a commercial charge, and charges are considerable, so can only give up. Then found the Ijkplayer, the function is also very powerful, finally chose Wei_leng based on Ijkplayer development Superplayer, here also to thank the selfless blogger, put his link superplayer

But because my layout is a little bit more complex, in the switch horizontal screen, and can not be perfect, and then reference a blogger's article, here also posted: video playback screen to switch

Okay, no more nonsense, let's go first, including gesture manipulation:


Below write down my collection procedure:1, go to GitHub search superplayer, download down its source code, unzip the file directory as:


Here we use Ijkplayerlibrary folder and Superplayerlibrary folder, it is not difficult to think of superplayerlibrary is to rely on Ijkplayerlibrary developed a custom player framework;

2. Import the above two folders as Moudle we need to use the player in engineering project and add dependencies in Gradle:

Compile project (': Superplayerlibrary ')

3. The following is our own reference to our custom player framework in our project:

In the XML file:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/activity_video_details "Android:layout_w        Idth= "Match_parent" android:layout_height= "match_parent" android:background= "@color/white" > <LinearLayout Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical "> <include layout=" @layout/titlebar_activities "/> <framelayout android:id=" @+id/vid Eo_screen "android:layout_width=" match_parent "android:layout_height=" 0DP "android:layout                _weight= "1" > <com.superplayer.library.superplayer android:id= "@+id/view_super_player" Android:layout_width= "Match_parent" android:layout_height= "match_parent"/> <im Ageview Android:id= "@+id/iv_cover_videodetails" android:layout_width= "match_parent" android:layout_height= "mat Ch_parent "android:scaletype=" Fitxy "android:src=" @drawable/start1 "/> <ima Geview android:id= "@+id/iv_play_videodetails" android:layout_width= "90px" and         roid:layout_height= "90px" android:layout_gravity= "center" android:src= "@drawable/play"/> </FrameLayout> <scrollview android:id= "@+id/scrollview_videodetails" Android : layout_width= "match_parent" android:layout_height= "0DP" android:layout_weight= "2" Androi d:scrollbars= "None" > <linearlayout android:layout_width= "match_parent" Android                    oid:layout_height= "match_parent" android:orientation= "vertical" > <linearlayout Android:layoUt_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center "android:orientation=" Horizontal "> <textview android:l Ayout_width= "0DP" android:layout_height= "Wrap_content" Android:layout_margin                        Bottom= "34px" android:layout_margintop= "34px" android:layout_weight= "1"                        android:gravity= "center" android:text= "@string/videodetails_details"                    Android:textcolor= "@color/green_text" android:textsize= "30px"/>                        <view android:layout_width= "1DP" android:layout_height= "Match_parent"                        android:background= "@color/gray_view_mine"/> <textview Android:id= "@+id/tv_liSt_videodetails "android:layout_width=" 0DP "android:layout_height=" Wrap_cont                        Ent "android:layout_marginbottom=" 34px "android:layout_margintop=" 34px " android:layout_weight= "1" android:gravity= "center" Androi                        d:text= "@string/videodetails_list" android:textcolor= "@color/black_free_more_home" Android:textsize= "30px"/> </LinearLayout> <view Andro Id:layout_width= "Match_parent" android:layout_height= "1DP" android:background= "@colo                    R/gray_view_mine "/> <textview android:layout_width=" Match_parent " android:layout_height= "1000DP" android:background= "@color/gray_view_mine" Androi      d:gravity= "Center"              android:text= "Testing"/> </LinearLayout> </ScrollView> </LinearLayout> <relativelayout android:id= "@+id/full_screen" android:layout_width= "Match_parent" Android:layo ut_height= "match_parent" android:gravity= "center" android:visibility= "Gone"/></relativelayout>

Because my layout is a bit complicated, in the horizontal screen, the playback can not achieve full screen, after referring to the above blogger's article, in the Java code implemented as follows:

Package Com.tianyunjuhe.peixunapp.activity;import Android.content.res.configuration;import Android.os.Bundle; Import Android.os.environment;import Android.view.view;import Android.view.viewgroup;import Android.widget.framelayout;import Android.widget.imageview;import Android.widget.relativelayout;import Android.widget.scrollview;import Android.widget.textview;import Com.superplayer.library.superplayer;import Com.superplayer.library.superplayermanage;import Com.tianyunjuhe.peixunapp.r;import Com.tianyunjuhe.peixunapp.dao.appmanager;import Com.tianyunjuhe.peixunapp.dao.baseactivity;import Butterknife. Bindview;import Butterknife. Onclick;public class Videodetailsactivity extends Baseactivity implements Superplayer.onnetchangelistener {@BindView (    R.id.full_screen) relativelayout fullscreen;    @BindView (r.id.iv_titlebar_back) ImageView ivtitlebarback;    @BindView (r.id.tv_titlebar_show) TextView tvtitlebarshow;    @BindView (r.id.iv_cover_videodetails) ImageView videocover;@BindView (r.id.iv_play_videodetails) ImageView videoplay;    @BindView (r.id.tv_list_videodetails) TextView videolist;    @BindView (R.id.view_super_player) Superplayer player;    Private Superplayer Msuperplayer;    Private Boolean islive;    Private String URL;    Private String path= environment.getexternalstoragedirectory (). GetAbsolutePath () + "/video.mp4";    @Override protected int Getcontentviewid () {return r.layout.activity_video_details;        } @Override protected void Initallmembersview (Bundle savedinstancestate) {initdata ();    Initplayer (); private void Initplayer () {if (islive) {player.setlive (true);//Set the address for live broadcast} player. Setnetchangelistener (TRUE)//Set monitor mobile network changes. Setonnetchangelistener (this)//callback to implement network changes. Onprepared (New Superplayer. Onpreparedlistener () {@Override public void onprepared () {/** * listener view The frequency is ready to complete start playing * can be in thisThe display and hide/videocover.setvisibility (view.gone) of the cover is processed;            Videoplay.setvisibility (View.gone);                 }}). OnComplete (New Runnable () {@Override public void run () {/**                 * Monitor whether the video has been played and finished.                * The operation of video playback can be handled here */videocover.setvisibility (view.visible);            Videoplay.setvisibility (view.visible); }}). Oninfo (New Superplayer.oninfolistener () {@Override public void oninfo (int-What, int ext RA) {/** * Listen for information about the video */}}). onError (New Superplayer.one                 Rrorlistener () {@Override public void onError (int-what, int extra) {/** * Monitor Video Playback failed callback */}}). Settitle (URL)//Set the name of the video. Play (URL);//start playing video pl Ayer.setscaletype (SUPERPLAYER.SCALETYPE_FITXY);   } private void InitData () {islive = Getintent (). Getbooleanextra ("Islive", false);    url = getintent (). Getstringextra ("url");    }/** * Implement network Monitoring */@Override public void Onwifi () {mtoast ("Current network environment is WiFi");    } @Override public void OnMobile () {mtoast ("Current network environment is mobile network");    } @Override public void OnDisconnect () {mtoast ("Network link Broken");    } @Override public void Onnoavailable () {mtoast ("Currently no network link"); } @OnClick ({r.id.iv_play_videodetails,r.id.iv_titlebar_back}) public void OnClick (view view) {switch (view.ge                TId ()) {case R.id.iv_play_videodetails:initplayer ();            Break                Case R.id.iv_titlebar_back:appmanager.getappmanager (). finishactivity ();            Break        Default:break;        }}/** * Overrides the activity's life cycle */@Override protected void OnPause () {super.onpause (); if (player!=null) {player.onpause ();        }} @Override protected void Onresume () {super.onresume ();        if (player!=null) {player.onresume ();        }} @Override protected void OnDestroy () {Super.ondestroy ();        if (player!=null) {Player.ondestroy (); }} @Override public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (n        Ewconfig); if (player! = null) {/** * invokes the player's listening method when monitoring changes in activity to achieve player size switching */Playe            R.onconfigurationchanged (Newconfig); Switch to small screen if (newconfig.orientation = = configuration.orientation_portrait) {Fullscreen.setvisibi                Lity (View.gone);                Fullscreen.removeallviews ();                Framelayout framelayout = (framelayout) Findviewbyid (R.id.video_screen);                Framelayout.removeallviews (); ViewGroup last = (viewgroup) player.GetParent ();//Find Videoitemview's parent class, and then remove if (last! = null) {//Last.removeallviews ();                Last.removeview (player);                } framelayout.addview (player); int mshowflags = View.system_ui_flag_layout_fullscreen | view.system_ui_flag_layout_hide_navigation |                view.system_ui_flag_layout_stable;            Fullscreen.setsystemuivisibility (Mshowflags);                } else {//switch to full screen viewgroup ViewGroup = (viewgroup) player.getparent ();                if (ViewGroup = = null) return;                Viewgroup.removeallviews ();                Fullscreen.addview (player);                Fullscreen.setvisibility (view.visible); int mhideflags = View.system_ui_flag_low_profile | View.system_ui_flag_layout_fullscreen                               | View.system_ui_flag_fullscreen | view.system_ui_flag_immersive | view.system_ui_flag_layout_hide_navigation |                View.system_ui_flag_hide_navigation;            Fullscreen.setsystemuivisibility (Mhideflags);        }} else {fullscreen.setvisibility (view.gone);            }} @Override public void onbackpressed () {if (player!=null&&player.onbackpressed ()) {        Return    } super.onbackpressed (); }}

Perfect for the switch between the screen.

Finally, you must add the following code to the activity of the player:

<activity android:name= ". Activity. Videodetailsactivity "    android:configchanges=" Keyboardhidden|orientation|screensize "    android: screenorientation= "Sensor"/>

To this basic realization of the screen switch!

Android video playback and toggle screen

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.