An overview of the

Recently READ zxing project, learned a lot of things. I recommend that you read it. There is a BeepManager class that implements a beep and vibration implementation.

buzzer

  1. Prepare an audio file such as beep.ogg. Ogg format is a sound compression format, similar to mp3. We’re going to play it, and there’s a buzz effect.
  2. The default audio channel registered for the activity. activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

The STREAM_MUSIC channel declared here is the multimedia playback channel. After registering, we can adjust the playback volume by using the volume button on the phone.

If this channel is not set, our activity’s default volume button handles will apply to the size of the phone’s ring tone.

3. Check the current ring tone mode or set it to situational mode.

GetRingerMode () — returns the current ringtone mode. For example, RINGER_MODE_NORMAL (normal), RINGER_MODE_SILENT (silent), and RINGER_MODE_VIBRATE (vibrate)

// If currently in ringtone mode, continue to prepare the following beep prompt operation, if in silent or vibrate mode. Don’t continue. Since the user chose the silent mode, we didn’t make any noise.

AudioManager audioService = (AudioManager) activity .getSystemService(Context.AUDIO_SERVICE); if (audioService.getRingerMode() ! = AudioManager.RINGER_MODE_NORMAL) { shouldPlayBeep = false; }Copy the code

4. Initialize the MediaPlayer object and specify the sound channel to play as STREAM_MUSIC. This points to the same channel as in the previous step.

MediaPlayer mediaPlayer = new MediaPlayer(); MediaPlayer. SetAudioStreamType (AudioManager. STREAM_MUSIC); Register events. After playing once, point back to the beginning of the stream file 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); }});Copy the code

Set up the data source and prepare 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;
Copy the code

5. Start playing

if (playBeep && mediaPlayer ! = null) { mediaPlayer.start(); }Copy the code

vibration

1. Declare permissions

In the AndroidManifest. Written in XML

<uses-permission android:name="android.permission.VIBRATE"/>
Copy the code

2. Get vibration service.

    Vibrator vibrator = 
    (Vibrator)     activity.getSystemService(Context.VIBRATOR_SERVICE);
Copy the code

3. Start the vibration.

vibrator.vibrate(VIBRATE_DURATION); public void playBeepSoundAndVibrate() { if (enableVibrate) { Vibrator vibrator = (Vibrator) activity .getSystemService(Context.VIBRATOR_SERVICE); Vibrate (VIBRATE_DURATION); // The first argument refers to an array of vibrational frequencies. Each group is divided into two groups. The first group is the waiting time and the second one is the vibration time. For example, [2000,500,100,400] will wait 2000 milliseconds, vibrate 500, wait 100, vibrate 400. Vibrator. Vibrate (new long[]{300,500},0); vibrator. }}Copy the code

Reference:

www.linuxidc.com/Linux/2011-…

www.linuxidc.com/Linux/2012-… Blog.csdn.net/barnett_zhu… www.cnblogs.com/j-turn/arch… This code refers to the Zxing open source project