Skip to main content

How to play a base64 encoded audio file in expo-av

· One min read

I spent today figuring out how to play a base64 audio encoded string using expo-av.

I could play audio fine on Android by directly passing in the base64 string URI

const { sound } = await Audio.Sound.createAsync({
uri: `data:audio/wav;base64,${base64AudioEncodedString}`
await sound.playAsync();

But when run on iOS, I got the error

The AVPlayerItem instance has failed with the error code -16041 and domain "CoreMediaErrorDomain".

So I figured out how to make audio play on both Android and iOS.

Basically createAsync seems to need to read from a file for iOS. So I used the expo-file-system.

import {
} from 'expo-file-system';

And did this

const tmpFilename = `${cacheDirectory}speech.wav`;
await writeAsStringAsync(tmpFilename, base64AudioEncodedString, {
encoding: EncodingType.Base64
const { sound } = await Audio.Sound.createAsync({ uri: tmpFilename });
await sound.playAsync();

-- Expo snack

And then it worked fine for both platforms 🚀