Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active July 18, 2023 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/a5fe650356d9bd8fc3e335a57d053379 to your computer and use it in GitHub Desktop.
Save magician11/a5fe650356d9bd8fc3e335a57d053379 to your computer and use it in GitHub Desktop.
How to play a base64 encoded audio file in expo-av
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>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment