When I run npx expo start
and run the application on my IOS device only plays audio from the earpiece instead of the speaker, any idea of why this is happening?
This is my code:
import { View, StyleSheet,Button } from 'react-native';
import React, { useState, useEffect } from 'react';
import { Audio } from 'expo-av';
export default function App() {
const [audioPermission, setAudioPermission] = useState(null);
useEffect(() => {
// Simply get recording permission upon first render
async function getPermission() {
await Audio.requestPermissionsAsync().then((permission) => {
console.log('Permission Granted: ' + permission.granted);
setAudioPermission(permission.granted)
}).catch(error => {
console.log(error);
});
}
// Call function to get permission
getPermission()
}, []);
async function playSound() {
try {
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true
})
const {sound} = await Audio.Sound.createAsync(require('./assets/beep.mp3'));
await sound.playAsync();
} catch (error) {
console.error('Failed to play sound', error);
}
}
return (
<View style={styles.container}>
<Button
title="play sound"
onPress={playSound}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
button: {
alignItems: 'center',
justifyContent: 'center',
width: 128,
height: 128,
borderRadius: 64,
backgroundColor: 'red',
},
});
I also try replacing to false the attributes inside the Audio.setAudioModeAsync like allowsRecordingIOS and playsInSilentModeIOS but that didn't work.
You have to set allowsRecordingIOS to false.