'Trying to play an audio file with react-native
I just started getting my hands on React Native and am currently trying to play an audio file in a test app. But it does not work at this point. Here is what I do:
First setting the app, on the terminal.
$ expo init TestAudio
$ cd TestAudio
$ npm install react-native-sound --save
$ npm run web
$ cp ...../TEST.mp3 .
Then here is how I set up the App.js file: (Its is mainly inspired from information I gathered from various samples and documents I found on the net, please tell me if something is wrong..)
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Button, View } from 'react-native';
import React, { useEffect } from 'react';
import Sound from 'react-native-sound';
export default function App() {
let control_Local, localSound = require('./TEST. mp3');
useEffect(() => {
Sound.setCategory('Playback', true);
return () => {
if (control_Local) control_Local.release();
};
}, []);
const playSound_Local = () => {
control_Local = new Sound(localSound, (error, _sound) => {
if (error) {
alert('error' + error.message);
return;
}
control_Local.play(() => {
control_Local.release();
});
});
}
return (
<View style={styles.container}>
<Button title='Play-Sound!'
onPress={playSound_Local}
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Unfortunately it does not work and this is the error message I get in the web console:
./node_modules/react-native/Libraries/Image/AssetSourceResolver.js:24:17
Module not found: Can't resolve '../Utilities/Platform'
22 |
23 | const PixelRatio = require('../Utilities/PixelRatio');
> 24 | const Platform = require('../Utilities/Platform');
| ^
25 |
26 | const invariant = require('invariant');
27 | index.js:1
Searching the net based on the error message, shows that it is not such an uncommon issue, but I didn't see any clear solution.
Has someone had the same problem to solve or has an idea of what to do?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
