Last active
December 5, 2024 00:12
-
-
Save magician11/a5fe650356d9bd8fc3e335a57d053379 to your computer and use it in GitHub Desktop.
How to play a base64 encoded audio file in expo-av
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { View } from 'react-native'; | |
import { | |
cacheDirectory, | |
writeAsStringAsync, | |
EncodingType | |
} from 'expo-file-system'; | |
import { Audio } from 'expo-av'; | |
import { Button } from 'react-native-paper'; | |
import base64AudioEncodedString from './assets/sounds'; | |
export default function App() { | |
const playSound = async () => { | |
const tmpFilename = `${cacheDirectory}speech.wav`; | |
await writeAsStringAsync(tmpFilename, base64AudioEncodedString, { | |
encoding: EncodingType.Base64 | |
}); | |
const { sound } = await Audio.Sound.createAsync({ uri: tmpFilename }); | |
await sound.playAsync(); | |
}; | |
return ( | |
<View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}> | |
<Button icon="play" mode="contained" onPress={() => playSound()}> | |
Play sound | |
</Button> | |
</View> | |
); | |
} |
have you ever found a solution
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expo snack: https://snack.expo.dev/@magician11/playing-base64-audio-in-expo-av
Write up: https://golightlyplus.com/blog/2023/07/playing-base64-encoded-audio-in-expo-av
Issue reference: expo/expo#20841 (comment)