Expo av plays audio only from earpiece instead of speaker

122 views Asked by At

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.

2

There are 2 answers

0
Max On

You have to set allowsRecordingIOS to false.

0
Huge Ponkce On

Set this, but not on the useEffect, it won't work there. You need to put this line right before creating the Sound object:

await Audio.setAudioModeAsync({ allowsRecordingIOS: false });

Like this:

await Audio.setAudioModeAsync({ allowsRecordingIOS: false });
const soundObject = new Audio.Sound();
await soundObject.loadAsync({ uri:`data:audio/wav;base64,${audioDatabase64String}` });
await soundObject.playAsync();