# OGG Files (Android)

Android devices with level 3 haptics support can play haptic feedback encoded in OGG Vorbis audio files. This allows to create haptic feedback that is synchronized with audio playback, both stored in the same file.

# Structure

Haptic-enabled OGG files (as exported from Hapticlabs Studio) contain three channels:

  1. Audio (left channel)
  2. Audio (right channel)
  3. Haptics

To identify to Android's MediaPlayer that the OGG file contains haptic feedback, the file needs to contain the following Vorbis comment:

Field name data
ANDROID_HAPTIC 1

The value of the ANDROID_HAPTIC field represents the count of haptic channels in the file (documentation).

The easiest way to obtain correctly structured OGG files is to export them from Hapticlabs Studio. To synchronize audio and haptic feedback, use the stereo track feature in Hapticlabs Studio.

# Playback

# React Native

The react-native-hapticlabs npm package provides a simple way to play back OGG files with haptic feedback on Android devices.

import { playOGG } from 'react-native-hapticlabs';

// Play an OGG file with haptic feedback
playOGG('path/to/file.ogg');

# Kotlin

To play back an OGG file with haptic feedback on an Android device, use the MediaPlayer class. The following code snippet demonstrates how to play an OGG file with haptic feedback:

val mediaPlayer = MediaPlayer()

// Enable haptic feedback
mediaPlayer.setAudioAttributes(
    AudioAttributes.Builder().setHapticChannelsMuted(false).build()
)

// Deactivate the HapticGenerator to ensure that it does not interfere with the haptics defined in the ogg
if (HapticGenerator.isAvailable()) {
    val generator = HapticGenerator.create(mediaPlayer.getAudioSessionId() ?: 0)
    generator.setEnabled(false)
}

// Set up the MediaPlayer instance
try {
    mediaPlayer.setDataSource("path/to/file.ogg")
} catch (e: IOException) {
    e.printStackTrace()
}
try {
    mediaPlayer.prepare()
} catch (e: IOException) {
    e.printStackTrace()
}

// Start playback
mediaPlayer.start()

// Don't forget to release the MediaPlayer instance when done