SoFunction
Updated on 2025-03-09

The implementation principle and reference code of beep prompt and vibration prompt in Android development

I've been studying the zxing project recently and learned a lot. It is recommended that you read it too. There is a BeepManager class inside, which implements a beep and vibration implementation. Let's see how he did it:
Beeping
1. Prepare an audio file such as:. The ogg format is a kind of sound compression format, similar to mp3. We were ready to play it and it produced a beeping effect.
2. The default audio channel registered for activity.
  (AudioManager.STREAM_MUSIC);
The channel declared as STREAM_MUSIC here is multimedia playback. After registration, we can use the volume keys on the mobile phone to adjust the playback sound.
If this channel is not set, our activity default volume button processing will act on the phone ringtone size.
3. Check the current ringtone mode, or become the situation mode.
Description: getRingerMode() — Returns the current ringtone mode. Such as RINGER_MODE_NORMAL (normal), RINGER_MODE_SILENT (silent), RINGER_MODE_VIBRATE (vibration)
Copy the codeThe code is as follows:

//If the ringtone mode is currently in, continue to prepare the following beep prompt operation, if it is mute or vibrating mode. Don't continue. Because the user has chosen the silent mode, we should not speak out.
AudioManager audioService = (AudioManager) activity
.getSystemService(Context.AUDIO_SERVICE);
if (() != AudioManager.RINGER_MODE_NORMAL) {
shouldPlayBeep = false;
}

4. Initialize the MediaPlayer object and specify that the playback sound channel is STREAM_MUSIC, which is consistent with the above steps and points to the same channel. MediaPlayer mediaPlayer = new MediaPlayer();
(AudioManager.STREAM_MUSIC);
Register events. When the playback is completed once, re-point to the beginning of the stream file to prepare for the next playback.
Copy the codeThe code is as follows:

// When the beep has finished playing, rewind to queue up another one.
mediaPlayer
.setOnCompletionListener(new () {
@Override
public void onCompletion(MediaPlayer player) {
(0);
}
});

Set the data source and prepare to play
Copy the codeThe code is as follows:

AssetFileDescriptor file = ().openRawResourceFd(
);
try {
((),
(), ());
();
(BEEP_VOLUME, BEEP_VOLUME);
();
} catch (IOException ioe) {
(TAG, ioe);
mediaPlayer = null;
}
return mediaPlayer;

5. Start playing
Copy the codeThe code is as follows:

if (playBeep && mediaPlayer != null) {
();
}

-----------------------------------------------------------------
shock
This is relatively simple. In two steps:
1. Declare permissions
Write in
Copy the codeThe code is as follows:

<uses-permission android:name=""/>

2. Obtain vibration services.
Copy the codeThe code is as follows:

Vibrator vibrator = (Vibrator) (Context.VIBRATOR_SERVICE);

3. Start the vibration.
Copy the codeThe code is as follows:

(VIBRATE_DURATION);

Copy the codeThe code is as follows:

public void playBeepSoundAndVibrate() {
if (enableVibrate) {
Vibrator vibrator = (Vibrator) activity
.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate once
(VIBRATE_DURATION);
//The first parameter refers to a vibration frequency array. Each two are a group, the first of each group is the waiting time and the second is the vibration time.
// For example [2000,500,100,400] will wait for 2000 milliseconds first, vibrate by 500, then wait for 100, vibrate by 400
//The second parameter, repeat refers to the starting of the cyclic vibration from the position of the first index (the first array parameter).
//The cycle will be maintained, we need to actively terminate with ()
//(new long[]{300,500},0);
}
}