Mobile video day, control screen up and down to change the volume changes, monitoring physical keys to change the volume

Source: Internet
Author: User
Tags gety

Code has been hosted on the code cloud, interested small partners can download see, IDE is Android studio 2.3.2

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

Common mobile phone players, have the screen up and down to change the volume size of the function, in this also implemented:

The principle is:

1, touch the screen from the finger, to leave the screen, calculate the height difference of the slide, and then take this height difference with the screen high contrast, and finally combined with the total volume, to get the change of volume, the specific formula is as follows:

Change volume = (sliding distance/total screen height) * Total Volume

Volume at end of swipe = volume at touch screen + changed volume

According to this to change the volume and Seekbar display progress

2, when the finger touches the screen, will be callback Ontouchevent method, in this method, there is a parameter event, this parameter can be obtained to the finger is on the slide or fall

3, when the finger touch the screen, at this time from Event.gety () to get started sliding the y-axis value, and get the sound value at this time

4, when the finger leaves the screen, according to Event.gety () to get away from the y-axis value, the previous y-axis value-then the y-axis value, you get the distance

5, according to the above algorithm to obtain the volume after receiving, and then set the corresponding volume and seekbar progress can be.


The specific code is as follows:

private float starty;  //the y-axis value that starts when you swipe to adjust the volume on the screen private float touchrange;   The screen is high, because it involves the screen switch, the time will take a small value private int touchvoice; //sliding volume  //The use of ontouchevent, Passing event events to the gesture recognizer, otherwise unable to trigger the gesture recognizer's callback method      @Override     public boolean  ontouchevent (motionevent event)  {         Dector.ontouchevent (Event);        switch  (Event.getAction ()) {             case motionevent.action_down:                 starty= Event.gety ();                 Currentvoice=audiomanager.getstreamvolume (audiomanager.stream_music);   //gets the volume at the start of the swipe                   touchrange=math.min (Screenheight,screenwidth);                 handler.removemessages (HIDEVIDEO);                 break;             case MotionEvent.ACTION_MOVE:                 float endy=event.gety ();      //Sliding distance                  float distance=starty-endy;  //relative sliding distance                  //float changevoice= (distance/ Touchrange) *maxvoice;  //Change the volume                  LOG.I (TAG,  " Ontouchevent: move change in Distance "+distance);                 //int voice=  (int)  math.min (Math.max (touchvoice+changevoice,0), Maxvoice)   //changed volume//                 if  (changevoice!=0) {  //Change the volume when the volume is not 0 o'clock.//                     setvoice (Voice,false );//                }                 break;             case MotionEvent.ACTION_UP:                 float upy=event.gety ();                 float upDistance=startY-upY;                 float  Changevoice= (updistance/touchrange) *maxvoice;  //Change the volume                  LOG.I (tag,  "ontouchevent:  the distance when the slide is removed" +upDistance );                int  voice=  (int)  math.min (Math.max (touchvoice+changevoice,0), Maxvoice),   //changed volume                  if  (changevoice!=0) {   //Change the volume when the volume is not 0 o'clock                      setvoice (Voice,false);                 }                 handler.sendemptymessagedelayed (hidevideo,3000);                 break;        }         return super.ontouchevent (event);     }


While the physical keys of Android also have the function of increasing and decreasing the volume, it is also necessary to listen to the callback method of physical keys to change the volume and the Seekbar progress bar.

/** *  when you press the phone to increase the volume, reduce the volume, the corresponding change volume and progress bar  *  @param  keyCode *  @param  event  *  @return  */@Overridepublic  boolean onkeydown (int keycode, keyevent  Event)  {    if  (Keycode==keyevent.keycode_volume_down) {         currentvoice--;        setvoice (CurrentVoice, FALSE);         handler.removemessages (Hidevideo);         handler.sendemptymessagedelayed (hidevideo,3000);         return true;    }else if  (Keycode==keyevent.keycode_ VOLUME_UP) {        currentvoice++;         setvoice (Currentvoice,false);         Handler.removemessages (hidevideo);   &nbSp;     handler.sendemptymessagedelayed (hidevideo,3000);         return  true;  //if return false, then seekbar change, at the same time the system volume bar also appears, not good-looking.     }    return super.onkeydown (keyCode, event);}


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

Mobile video day, control screen up and down to change the volume changes, monitoring physical keys to change the volume

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.