The implementation principle and reference code of the buzzer prompt and vibration prompt developed by android

  • 2020-05-09 19:16:11
  • OfStack

Recently, I have been reading zxing project and learned a lot. I recommend you read it. There is an BeepManager class that implements a buzz and vibration. Let's take a look at how he does it:
buzzer
1. Prepare an audio file such as beep.ogg. ogg is one of the sound compression formats, like mp3. We're going to play it, and it's going to buzz.
2. The default audio channel registered for activity.
activity. setVolumeControlStream (AudioManager. STREAM_MUSIC);
Here is the channel declared as STREAM_MUSIC, which is multimedia play. After registration, we can adjust the sound level by using the volume button on the phone.
If we do not set this channel, our default volume button activity will be applied to the volume of the ringtone.
3. Check the current ringtone mode, or become a situational mode.
Description: getRingerMode() -- returns the current ringtone mode. Such as RINGER_MODE_NORMAL (normal), RINGER_MODE_SILENT (silent), RINGER_MODE_VIBRATE (vibration)
 
  // If you are currently in ringtone mode, continue to prepare the following   Buzzer cue operation, if mute or vibrate mode. Don't go on. Since the user has chosen the silent mode, we don't speak.  
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 sound channel to be played as STREAM_MUSIC, which points to the same channel as in step 1 above. MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC);
Register events. After playing once, redirect to the beginning of the stream file to prepare for the next play.
 
  // 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 get ready to play
 
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 playing
 
if (playBeep && mediaPlayer != null) { 
mediaPlayer.start(); 
} 

-----------------------------------------------------------------
vibration
This one is easy. Step two:
1. Declare permissions
In AndroidManifest xml in writing
 
  <uses-permission android:name="android.permission.VIBRATE"/> 

2. Get vibration service.
 
  Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); 

3. Start the vibration.
 
  vibrator.vibrate(VIBRATE_DURATION); 

 
public void playBeepSoundAndVibrate() { 
if (enableVibrate) { 
Vibrator vibrator = (Vibrator) activity 
.getSystemService(Context.VIBRATOR_SERVICE); 
// vibration 1 time  
vibrator.vibrate(VIBRATE_DURATION); 
// The first 1 Is a parameter that refers to 1 An array of vibrational frequencies. Two for each 1 Group, the first of each group 1 Is the waiting time, the 2 Is the vibration time.  
//  Such as  [2000,500,100,400], Will be waiting for 2000 Milliseconds, vibration 500 And to wait for 100 , the vibration 400 
// The first 2 The parameters, repest Refer to from   Index number (no 1 Array parameters)   The position of the loop begins to vibrate.  
// will 1 Straight through the loop, we need to use  vibrator.cancel() Take the initiative to stop  
//vibrator.vibrate(new long[]{300,500},0); 
} 
} 

Related articles: