Mobile audio Nineth day, control video full screen playback and exit full screen playback, volume adjustment button to control the video volume and mute implementation

Source: Internet
Author: User

Code to host to the code cloud, interested in small partners can download to see

Https://git.oschina.net/joy_yuan/MobilePlayer

One, full-screen video playback and exit full screen

System default Videoview class, there is no resizing method, so you need to customize a class, inherit Videoview, then rewrite the three construction methods inside, and then customize a method to adjust the video playback page size.

1. layout file

Also, in the layout file for the video playback, the Videoview layout refers to the custom class above.

<?xml version= "1.0"  encoding= "Utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android "               android:orientation= "Vertical"                  android:gravity= "Center"                  android:background= "#000000"                android:layout_width= "Match_parent"                android:layout_height= "Match_parent" >     <com.yuanlp.mobileplayer.view.MyVideoView         Android:layout_centerinparent= "true"         android:id= "@+id/videoview "        android:layout_width= "Match_parent"         android:layout_ height= "Match_parent"/>    <include layout= "@layout/media_controller"   Android:id= "@+id/media_controller"/></relativelayout>

2, custom classes

package com.yuanlp.mobileplayer.view;import android.content.context;import  android.util.attributeset;import android.view.viewgroup;import android.widget.videoview;/** *  Created by  Yuriping  on 2017/7/19. */public class myvideoview extends  videoview {    //is used when creating in code, i.e. New    public myvideoview (Context context)  {        this (context,null);     }    //when using this class in a layout file, the system automatically uses this construction method     public  Myvideoview (context context, attributeset attrs)  {        this (context, attrs,0);    }    /**      *  Use this class when you need a style      *  @param  context      *  @param  attrs     *  @param  defStyleAttr     */    public  Myvideoview (context context, attributeset attrs, int defstyleattr)  {         super (context, attrs, defstyleattr);     }     /**     *  Measurement      *  @param  widthMeasureSpec     *  @param  heightMeasureSpec      */     @Override     protected void onmeasure (int &NBSP;WIDTHMEASURESPEC,&NBSP;INT&NBSP;HEIGHTMEASURESPEC)  {         super.onmeasure (Widthmeasurespec, heightmeasurespec);         setmeasureddimension (Widthmeasurespec, heightmeasurespec);    }     /**     *  Setting the width      *  @param of the video  width     *   @param  height     */    public void  Setvideosize (int width,int height) {         Viewgroup.layoutparams params=getlayoutparams ();         params.width=width;        params.height=height;         setlayoutparams (params);     }}

3. Set up full screen playback

Get the length and width of the video in the callback method where you want to play the video.

/** *  ready to play when listening  */class MyOnPreparedListener implements  mediaplayer.onpreparedlistener {         @Override      public void onprepared (MEDIAPLAYER&NBSP;MP)  {         videoview.start ();  //start playing         duration =  videoview.getduration ();   //get total video length          Seekbarvideo.setmax (Duration)    //set the maximum playback progress         // Send a message to update the video Progress         handler.sendemptymessage (PROGRESS);         //set the total video duration for the display          Tvduration.settext (Utils.stringfortime (duration))         //The default entry to play, hide         media_controller.setvisibIlity (View.gone);         isshow=false;  //set to Hidden          //get the width and height of the player playing video          Videoheight=mp.getvideoheight ();         videowidth=mp.getvideowidth ();        // videoview.setvideosize (Mp.getvideowidth (), Mp.getVideoHeight ( );         setvideotype (defaultscreen);     }}

Using the displaymetrics of the system to obtain the width and height of the mobile phone screen;

/** * Gets the width height of the phone screen pixels */displaymetrics displaymetrics=new displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics); screenwidth=displaymetrics.widthpixels;screenheight=displaymetrics.heightpixels;

4, when double-click the screen, to determine whether the current is full screen, define a global variable IsFullScreen, the default false to determine whether the full screen,

4.1 If it is not full screen, then set to full screen, set IsFullScreen to True,

Set the width of the screen to the width of the video, the height of the screen, set the height of the video

The bottom right corner of the video is the full-screen icon changed

else{    //is the default, set to Full screen     setvideotype (fullscreen);} ... Private void setvideotype (Int screen)  {  if  (screen==fullscreen) {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1, setting video screen size          Videoview.setvideosize (screenwidth,screenheight); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2, setting button change        btvideoswitchscreen.setbackgroundresource (R.drawable.bt_video_switch_screen_default_ selector);       //3, setting isfullscreen=true;       isfullscreen=true;  }else if  (Screen==defaultscreen) {         int mvideowidth=videowidth;  //Video true width          int mvideoheight=videoheight; //Video true Height        Videoview.setvideosize (mvideowidth,mvideoheight);      &nbSp;btvideoswitchscreen.setbackgroundresource (R.drawable.bt_video_switch_screen_full_selector);       isfullscreen=false;  }}

4.2 If it is already full screen, set IsFullScreen to False, set to normal screen size

The width height of the player is set to the width height in the callback method based on the previously obtained preparation playback

if (IsFullScreen) {//Is full screen, then set to default Setvideotype (Defaultscreen);}


Second, set Seekbarvoice this control player volume

1, the player volume can be audiomanager to get the instance control

/** * Instantiate Volume Manager */audiomanager= (Audiomanager) Getsystemservice (context.audio_service); currentvoice= Audiomanager.getstreamvolume (Audiomanager.stream_music);  Current volume maxvoice=audiomanager.getstreammaxvolume (audiomanager.stream_music); Maximum volume/2, set the maximum and current value of the Seekbarvoice volume progress bar, note that the volume Max is 15.//set volume monitor Seekbarvoice.setonseekbarchangelistener (new Voiceonseekbarchangelistener ());//Set Volume Seekbarvoice.setmax (maxvoice); seekbarvoice.setprogress (CurrentVoice);

3. Create a change in the volume progress bar monitoring class

/** *  Volume progress bar Change monitoring  */class VoiceOnSeekBarChangeListener implements  seekbar.onseekbarchangelistener {     @Override     public  Void onprogresschanged (Seekbar seekbar, int progress, boolean fromuser)  {         if  (Fromuser) {             if  (progress>0) {                 isMuteVoice=false;             }else{                 ismutevoice=true;            }             setvoice (Progress,isMuteVoice);         }     }     @Override     public void  Onstarttrackingtouch (Seekbar seekbar)  {         Handler.removemessages (hidevideo);    }     @Override      public void onstoptrackingtouch (Seekbar seekbar)  {         handler.sendemptymessagedelayed (hidevideo,3000);     }}public void  setvoice (Int progress,boolean ismutevoice)  {    if  (Ismutevoice) {        seekbarvoice.setprogress (0);         //Volume controller Controls Volume Change         //audiomanager.setstreamvolume ( audiomanager.stream_music,progress,1)   //The third parameter is set to 1, indicating the system volume progress          audiomanager.setstreamvolumE (audiomanager.stream_music,0,0);   //The third parameter is set to 1, indicating the system volume progress     }else {         //Volume controller controls volume changes         // Audiomanager.setstreamvolume (audiomanager.stream_music,progress,1);   //The third parameter is set to 1, indicating the system volume progress          audiomanager.setstreamvolume (audiomanager.stream_music,progress,0);         seekbarvoice.setprogress (Progress);         currentvoice=progress;  //assign the volume to the current volume        &NBSP;&NBSP;LOG.D (tag,  "setvoice:  current Sound" +currentvoice);         system.out.println ("setvoice ----------Current Sound" +currentvoice);     }}

Control the system volume and set the progress on the Seekbar with the progress value when the progress bar changes.


4. When the sound button is clicked, the mute or not is set.

if  ( v == btnVoice )  {             // Handle clicks for btnVoice             isMuteVoice=!isMuteVoice;            setvoice (Currentvoice,ismutevoice);//             if  (Ismutevoice) {//                 //is mute, set to not mute//                 isMuteVoice=false;//                 seekbarvoice.setprogress (Currentvoice);//                 audiomanager.setstreamvolume (AudioManager.STREAM_ music,currentvoice,0);/            }else {//                 //is not muted and is set to mute//                 isMuteVoice=true;//                 seekbarvoice.setprogress (0 );//                 Audiomanager.setstreamvolume (audiomanager.stream_music,0,0);////             }        }

At this point, full-screen playback of the video, volume changes and mute has been done

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1949571

Mobile audio Nineth day, control video full screen playback and exit full screen playback, volume adjustment button to control the video volume and mute implementation

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.