Haptic clips are created with either the Studio desktop app or the Studio web app. A haptic clip contains high-definition, device-agnostic haptic data that the Studio framework for iOS uses to create an optimal haptic effect on playback devices. The playback of these haptic clips requires only three simple steps:
- Load the haptic clip into the
LofeltHapticsobject with the
- Start playback of the haptic clip with the
- Stop playback of the haptic clip with the
stop()method (which is only needed when you wish to stop the haptic clip before it has finished playing).
func load(_ data: String) throws
load() method allows you to load the contents of a haptic clip into memory before triggering playback with
play(). This preloading procedure is crucial for achieving tight synchronization between a haptic clip and its associated audio sample.
func play() throws
play() method initiates haptic playback of the haptic clip currently loaded into the
Playback of the haptic effect always starts from the beginning of the haptic clip and automatically stops when playback reaches the end of the haptic clip. However, you can stop playback of the haptic effect prematurely with the
func stop() throws
LofeltHaptics object is currently playing a haptic clip, the
stop() method terminates playback. If a haptic clip is not playing, the
stop() method will have no effect.
stop() method leaves the currently loaded haptic clip in memory. A subsequent
play() method will play the same haptic clip again starting from the beginning.
let haptics = try? LofeltHaptics.init() // Fetch a Haptic Clip from the asset catalog. let hapticClip = NSDataAsset(name: "haptic_clip.haptic") // Load its data into an NSString. let hapticData = NSString(data: hapticClip!.data , encoding: String.Encoding.utf8.rawValue) // Load it into the LofeltHaptics object as a String. try? haptics.load(hapticData! as String) // Play audio and haptics (audio must be played first). audioPlayer?.play() haptics?.play()
hapticDatais of type
Stringand is the contents of a haptic clip created with Studio.
audioPlayeris some kind of audio player, for example,
It is important to play the audio before the haptic effect for optimal audio/haptic synchronicity.
sdk/examples/ios folder within the Lofelt Studio download, there is an example project for playing haptic clips on iPhone called
LofeltHapticsExamplePreAuthored. To run the example, open the project in Xcode, target your iPhone, and build. The example provides two buttons that, when pressed and released, will trigger audio files and accompanying haptic clips.