Using a Pixel 4 running Android 12 I try to use the new HapticGenerator API (API level >= 31) to generate haptic feedback from an audio track.
However, I observe a random problem where the audio track is played during the first milliseconds at maximum volume. (Even if the volume of the phone is fully muted).
I've made a sample application that reproduce the problem that I'm facing.
Relevant part of the code that setup the MediaPlayer and the HapticGenerator:
private MediaPlayer player = null;
private HapticGenerator hapticGenerator = null;
private void releasePlayer() {
if (player != null) {
player.release();
}
if (hapticGenerator != null) {
hapticGenerator.release();
}
}
private void play() {
button.setEnabled(false);
releasePlayer();
player = MediaPlayer.create(getApplicationContext(), R.raw.sniper_rifle);
if (HapticGenerator.isAvailable()) {
hapticGenerator = HapticGenerator.create(player.getAudioSessionId());
hapticGenerator.setEnabled(true);
}
player.setOnCompletionListener(mp -> button.setEnabled(true));
player.start();
}
As you can see the code is quite simple but randomly the audio track start at full volume during couple of milliseconds.
- Anyone already experienced this problem?
- Any workaround or suggestion?
- If you have another phone model that support the HapticGenerator API do you observe the same problem? I'm wondering if the problem is specific to the pixel 4 implementation.