Added in API level 31

HapticGenerator

open class HapticGenerator : AudioEffect, AutoCloseable
kotlin.Any
   ↳ 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 android.media.MediaPlayer#getAudioSessionId() for details on audio sessions.

See android.media.audiofx.AudioEffect class for more details on controlling audio effects.

Summary

Inherited constants
Public methods
open Unit

Release the resources that are held by the effect.

open static HapticGenerator
create(audioSession: Int)

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

open static Boolean

open Unit

Releases the native AudioEffect resources.

open Int
setEnabled(enabled: Boolean)

Enable or disable the effect.

Inherited functions
Inherited properties

Public methods

close

Added in API level 31
open fun close(): Unit

Release the resources that are held by the effect.

Exceptions
java.lang.Exception if this resource cannot be closed

create

Added in API level 31
open static fun create(audioSession: Int): HapticGenerator

Creates a HapticGenerator and attaches it to the given audio session. Use android.media.AudioTrack#getAudioSessionId() or android.media.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.
Return
HapticGenerator HapticGenerator created or null if the device does not support HapticGenerator or the audio session is invalid.
Exceptions
java.lang.IllegalArgumentException when HapticGenerator is not supported
java.lang.UnsupportedOperationException when the effect library is not loaded.
java.lang.RuntimeException for all other error

isAvailable

Added in API level 31
open static fun isAvailable(): Boolean
Return
Boolean true if the HapticGenerator is available on the device.

release

Added in API level 31
open fun release(): Unit

Releases the native AudioEffect resources.

setEnabled

Added in API level 31
open fun setEnabled(enabled: Boolean): Int

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

Parameters
enabled Boolean: the requested enable state
Return
Int SUCCESS in case of success, ERROR_INVALID_OPERATION or ERROR_DEAD_OBJECT in case of failure.
Exceptions
java.lang.IllegalStateException