How to play an mp3 file on the Internet?

#1

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',
    },
});

0 Likes

#2

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

0 Likes

#4

It works now, thank you.

0 Likes

closed #5

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

0 Likes