HapticGenerator

public class HapticGenerator
extends AudioEffect implements AutoCloseable

java.lang.Object
   ↳ android.media.audiofx.AudioEffect
     ↳ android.media.audiofx.HapticGenerator


Haptic Generator(HG).

HG is an audio post-processor which generates haptic data based on the audio channels. The generated haptic data is sent along with audio data down to the audio HAL, which will require the device to support audio-coupled-haptic playback. In that case, the effect will only be created on device supporting audio-coupled-haptic playback. Call HapticGenerator#isAvailable() to check if the device supports this effect.

An application can create a HapticGenerator object to initiate and control this audio effect in the audio framework.

To attach the HapticGenerator to a particular AudioTrack or MediaPlayer, specify the audio session ID of this AudioTrack or MediaPlayer when constructing the HapticGenerator.

See MediaPlayer.getAudioSessionId() for details on audio sessions.

See AudioEffect class for more details on controlling audio effects.

Summary

Inherited constants

Inherited fields

Public methods

void close()

Release the resources that are held by the effect.

static HapticGenerator create(int audioSession)

Creates a HapticGenerator and attaches it to the given audio session.

static boolean isAvailable()
void release()

Releases the native AudioEffect resources.

int setEnabled(boolean enabled)

Enable or disable the effect.

Inherited methods

Public methods

close

Added in API level 31
public void close ()

Release the resources that are held by the effect.

create

Added in API level 31
public static HapticGenerator create (int audioSession)

Creates a HapticGenerator and attaches it to the given audio session. Use AudioTrack.getAudioSessionId() or MediaPlayer.getAudioSessionId() to apply this effect on specific AudioTrack or MediaPlayer instance.

Parameters
audioSession int: system wide unique audio session identifier. The HapticGenerator will be applied to the players with the same audio session.

Returns
HapticGenerator HapticGenerator created or null if the device does not support HapticGenerator or the audio session is invalid.

Throws
IllegalArgumentException when HapticGenerator is not supported
UnsupportedOperationException when the effect library is not loaded.
RuntimeException for all other error

isAvailable

Added in API level 31
public static boolean isAvailable ()

Returns
boolean true if the HapticGenerator is available on the device.

release

Added in API level 31
public void release ()

Releases the native AudioEffect resources.

setEnabled

Added in API level 31
public int setEnabled (boolean enabled)

Enable or disable the effect. The effect can only be enabled if the caller has the Manifest.permission.VIBRATE permission.

Parameters
enabled boolean: the requested enable state

Returns
int AudioEffect.SUCCESS in case of success, AudioEffect.ERROR_INVALID_OPERATION or AudioEffect.ERROR_DEAD_OBJECT in case of failure.