How do I adjust to play audio even the headphones are not disconnected?

#1

Following is my code, the audio only plays when the disconnected are disconnected. How do I adjust to playing even the headphones are not disconnected?

import React from 'react';
import {StyleSheet, Text, View, Button} from 'react-native';
import Audio from 'expo';

export default class App extends React.Component {
    componentDidMount = async () => {
        onPlaybackStatusUpdate = null
        initialStatus = {}
        downloadFirst = true
        source = {uri: 'https://www.word.today/uploads/word/pronunciation_audio/727/consistent.mp3'}
        const soundObject = new Expo.Audio.Sound();
        soundObject.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
        await soundObject.loadAsync(source, initialStatus, downloadFirst);
        soundObject.playAsync();
        console.log("page loaded");
    }


    onPressLearnMore = async () => {
        onPlaybackStatusUpdate = null
        initialStatus = {}
        downloadFirst = true
        source = {uri: 'https://www.word.today/uploads/word/pronunciation_audio/727/consistent.mp3'}
        const soundObject = new Expo.Audio.Sound();
        soundObject.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
        await soundObject.loadAsync(source, initialStatus, downloadFirst);
        soundObject.playAsync();
        console.log("this is a test");
    }

    render() {
        return (
            <View style={styles.container}>
                <Button title="play audio" color="blue" onPress={this.onPressLearnMore}/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
    },
});

#2

Hey @vincentwordtoday,

You’ll need to set your audio mode value mentioned here: https://docs.expo.io/versions/v30.0.0/sdk/audio#playthroughearpieceandroid--set-to-true-to-route

Cheers,

Adam

1 Like
closed #3

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.