import AlphService from './AlphService';
export default class MyClass extends React.Component {
alphService;
constructor(props) {
super(props);
alphService = new AlphService();
this.state = alphService.getState();
}
componentWillMount() {
this.play(this.state.sound);
}
play(source) {
console.log(source);
Audio.setIsEnabledAsync(true);
const sound = new Audio.Sound();
const play_sound = (async () => {
await sound.loadAsync(require(source)); //Error Here
await sound.playAsync();
})();
}
}
AlphService.js
export default class alphService {
stateObj = {
sound: ""
};
getState(){
this.stateObj.sound = "../assets/sounds/c.mp3";
return this.stateObj;
}
}
ERROR: components/Alphabet.js:Invalid call at line 51: require(source)
and also try
I return require(soundPathVar)
object from getState()
method and remove required from play()
, then same error.
I am beginner in react-native and I want to try creating a dynamic sound file path. So can you please help me if I am doing wrong. Thank in advance.