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.
 
AudioManager audioManager = context.getSystemService(AudioManager.class);
 player = MediaPlayer.create(
         context,
         audioUri,
         new AudioAttributes.Builder().setHapticChannelsMuted(false).build(),
         audioManager.generateAudioSessionId()
 );
 if (HapticGenerator.isAvailable()) {
     HapticGenerator.create(player.getAudioSessionId()).setEnabled(true);
 }
 player.start();
 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
create
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
public static boolean isAvailable ()
| Returns | |
|---|---|
| boolean | true if the HapticGenerator is available on the device. | 
setEnabled
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.SUCCESSin case of success,AudioEffect.ERROR_INVALID_OPERATIONorAudioEffect.ERROR_DEAD_OBJECTin case of failure. | 
