Implementation principle and reference code of beep and vibration prompts in android Development

Source: Internet
Author: User

I have learned a lot about zxing recently. We recommend that you read it. There is a BeepManager class to implement the implementation of a beep sound and vibration. Let's take a look at how he did it:
Beiming
1. Prepare an audio file such as beep.ogg. Ogg is a type of audio compression format, similar to mp3. We are going to play it, and it will produce a beep effect.
2. Default audio channel registered for activity.
Activity. setVolumeControlStream (AudioManager. STREAM_MUSIC );
It is declared as the STREAM_MUSIC channel, that is, multimedia playback. After registration, we can adjust the playback sound size by using the volume key on the mobile phone.
If you do not set this channel, the default volume button of our activity will apply to the phone tone.
3. Check the current tone mode or change to the scene mode.
Note: getRingerMode () -- returns the current ringtone mode. Such as RINGER_MODE_NORMAL (normal), RINGER_MODE_SILENT (mute), RINGER_MODE_VIBRATE (vibrate) Copy codeThe Code is as follows: // if it is in the ring tone mode, continue to prepare the following beep tone operation, if it is in the mute or vibrate mode. Do not continue. Because the user selects the silent mode, we should not speak out.
AudioManager audioService = (AudioManager) activity
. GetSystemService (Context. AUDIO_SERVICE );
If (audioService. getRingerMode ()! = AudioManager. RINGER_MODE_NORMAL ){
ShouldPlayBeep = false;
}

4. initialize the MediaPlayer object and specify the playing sound channel as STREAM_MUSIC. This is consistent with the preceding step and points to the same channel. MediaPlayer mediaPlayer = new MediaPlayer ();
MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
Register an event. After the playback is completed, point to the beginning of the stream file again to prepare for the next playback.Copy codeThe Code is as follows: // When the beep has finished playing, rewind to queue up another one.
MediaPlayer
. SetOnCompletionListener (new MediaPlayer. OnCompletionListener (){
@ Override
Public void onCompletion (MediaPlayer player ){
Player. seekTo (0 );
}
});

Set the data source and prepare for playbackCopy codeThe Code is as follows: AssetFileDescriptor file = activity. getResources (). openRawResourceFd (
R. raw. beep );
Try {
MediaPlayer. setDataSource (file. getFileDescriptor (),
File. getStartOffset (), file. getLength ());
File. close ();
MediaPlayer. setVolume (BEEP_VOLUME, BEEP_VOLUME );
MediaPlayer. prepare ();
} Catch (IOException ioe ){
Log. w (TAG, ioe );
MediaPlayer = null;
}
Return mediaPlayer;

5. Start PlayingCopy codeThe Code is as follows: if (playBeep & mediaPlayer! = Null ){
MediaPlayer. start ();
}

-----------------------------------------------------------------
Vibration
This is relatively simple. In two steps:
1. Declare Permissions
Write in AndroidManifest. xmlCopy codeThe Code is as follows: <uses-permission android: name = "android. permission. VIBRATE"/>

2. Obtain the vibration service.Copy codeThe Code is as follows: Vibrator vibrator = (Vibrator) activity. getSystemService (Context. VIBRATOR_SERVICE );

3. Start the vibration.Copy codeThe Code is as follows: vibrator. vibrate (VIBRATE_DURATION );

Copy codeThe Code is as follows: public void playBeepSoundAndVibrate (){
If (enableVibrate ){
Vibrator vibrator = (Vibrator) activity
. GetSystemService (Context. VIBRATOR_SERVICE );
// Shake once
Vibrator. vibrate (VIBRATE_DURATION );
// The first parameter refers to an array of vibration frequencies. Each two is a group. The first in each group is the waiting time, and the second is the shaking time.
// For example, if the value is [2000,500,100,400], the system will wait for 2000 milliseconds, shake 500, then wait for 100, shake 400
// The second parameter, repest refers to the cyclic vibration starting from the position of the first index (the first array parameter.
// Keep the loop. We need to use vibrator. cancel () to stop it.
// Vibrator. vibrate (new long [] {300,500}, 0 );
}
}

Related Article

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.