The principle mechanism of the sound realization of the key vibration key in Android development

Source: Internet
Author: User

If our Android app wants to invoke the system's vibration service when the button is pressed, we have to androidmainfest.xml it with the appropriate permissions.

<uses-permission android:name= "Android.permission.VIBRATE"/>


Then it's


Vibrator Vibrator = (Vibrator) getsystemservice (Vibrator_service);
Vibrator.vibrate (3000);
Set the vibration period of the vibrate
Vibrator.vibrate (New long[]{1000,2000,3000,4000}, 0);

Here again on the Internet to find a good way to write the vibration of the class

package com.lxb.switchdemo;      import android.app.activity;   Import  android.app.Service;   import android.os.vibrator;   import 
android.widget.linearlayout;     /**   *  mobile phone vibration tools    *    *  @author  Administrator   *    */   public class  vibratorutil {          /**       * &NBSP;FINAL&NBSP;ACTIVITY&NBSP;ACTIVITY&NBSP: The activity instance of the method is invoked  long milliseconds : the length of the vibration, in milliseconds.   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*&NBSP;LONG[]&NBSP;PATTERN&NBSP: Custom Vibration mode  . The meaning of the numbers in the array is [static length, length of vibration, length of stillness, length of vibration ...] The length of the unit is milliseconds        * boolean isRepeat :  whether repeated vibrations, if true, repeated vibrations, If it is false, only once        */          public  static voiD vibrate (final activity activity, long milliseconds)  {            Vibrator vib =  (vibrator)  activity                    .getsystemservice ( Service.vibrator_service);           vib.vibrate (milliseconds );       }          public static  void vibrate (final activity activity, long[] pattern,                boolean isrepeat)  {    
       Vibrator vib =  (Vibrator)  activity                   . Getsystemservice (Service.vibratOr_service);           vib.vibrate (pattern, isRepeat  ? 1 : -1);       }  }


Of course, when called in your activity, you only need to add the

Vibratorutil.vibrate (switch_demoactivity.this) to the onclick;

Simple vibration mechanism can be implemented

down to summarize the mechanism implementation of the key sound,

public class mainactivity extends activity {         private Button mButton01;        private soundpool  sp;//declares a soundpool        private int music;//defines an integral type with load () ; To set suondid             @Override          public void oncreate (bundle savedinstancestate)  {     
          super.oncreate (savedinstancestate);   
        setcontentview (R.layout.activity_main);            mbutton01= (Button) Findviewbyid (R.id.mbutton01);             sp= new soundpool (10, AudioManager.STREAM _SYSTEM,&NBSP;5);//The first parameter is the sameThe maximum number of data streams to play, the second data stream type, and the third for sound quality             music =  sp.load (this, r.raw.start, 1);  //put your voice footage into Res/raw, the 2nd parameter is the resource file, and the 3rd is the priority of the music                 mbutton01.setonclicklistener (new  Onclicklistener () {                @ override               public void  onclick (view v)  {                  sp.play (music, 1, 1, 0, 0, 1);            }       }     }  }


Raw is a new folder under Res, which is a direct resource file that does not need to be compiled, and, of course, to avoid the need to recognize the key voice in some machines, it is best to convert the audio format that needs to be loaded into the Ogg format.

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.