How to play an mp3 file on the Internet?

Following is my code and error message, please help.

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

export default class App extends React.Component {
    onPressLearnMore = () => {
        console.log("this is a test");
        const soundObject = new Expo.Audio.Sound();
        try {
            await soundObject.loadAsync(require('https://www.word.today/uploads/word/pronunciation_audio/727/consistent.mp3'));
            await soundObject.playAsync();
            // Your sound is playing!
        } catch (error) {
            // An error occurred!
        }
    }

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

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

Hey @vincentwordtoday,

As the error message indicates, await is a reserved word only to be used in async functions. You’ll need to change your function from onPressLearnMore = () => {} to onPressLearnMore = async () => {}

Cheers,

Adam

It works now, thank you.

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