Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 8 of 10
Haptics & Audio
Haptic feedback and audio make interactions feel alive. A subtle tap feedback on a button press, a success vibration on form submit, and error tones all dramatically improve UX with minimal code.
expo-haptics & expo-av
import * as Haptics from 'expo-haptics';
import { Audio } from 'expo-av';
// npx expo install expo-haptics expo-av
// ---- Haptic presets ----
// Impact: physical tap/click sensation
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light); // subtle
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium); // moderate
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy); // strong
// Notification: status feedback
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success); // β Done
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning); // β οΈ
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error); // β Failed
// Selection: scrolling through a picker
await Haptics.selectionAsync();
// ---- Audio: sound effect on action ----
async function playSoundEffect(assetPath: number) {
const { sound } = await Audio.Sound.createAsync(assetPath);
await sound.playAsync();
// Unload after playing (free memory)
sound.setOnPlaybackStatusUpdate(status => {
if ('didJustFinish' in status && status.didJustFinish) sound.unloadAsync();
});
}
// Usage
await playSoundEffect(require('../assets/sounds/coin.mp3'));
// ---- Preload sounds for fast playback ----
const SOUND_CACHE = new Map<string, Audio.Sound>();
async function preloadSound(key: string, assetPath: number) {
const { sound } = await Audio.Sound.createAsync(assetPath);
SOUND_CACHE.set(key, sound);
}
async function playPreloaded(key: string) {
const sound = SOUND_CACHE.get(key);
if (!sound) return;
await sound.setPositionAsync(0); // rewind to start
await sound.playAsync();
}Tip
Tip
Practice Haptics Audio in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Enhancing tactile UX with Taptic Engine vibration and low-latency audio
Practice Task
Note
Practice Task - (1) Write a working example of Haptics Audio from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Haptics Audio is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?