问题
I want to play a sound in React Native.
I have try to read here in zmxv/react-native-sound, but as a beginner like me, that's documentation make me confused how to apply that in React Native code.
Before I have try this one to make react native play sound on event and make a code like this:
import React, { Component } from 'react'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
const Sound = require('react-native-sound')
export default class MovieList extends Component {
handlePress() {
// Play some sound here
let hello = new Sound('motorcycle.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log(error)
}
})
hello.play((success) => {
if (!success) {
console.log('Sound did not play')
}
})
}
render() {
const { movie } = this.props
return (
<TouchableOpacity onPress={this.handlePress.bind(this)}>
<View>
<Text>Start</Text>
</View>
</TouchableOpacity>
)
}
}
And this is where I put my audio:
MyProject/android/app/src/main/res/raw/motorcycle.mp3
Project structure
So, what's wrong in my code?
回答1:
This will preload the sound and When you press the play it will play it.
export default class MovieList extends Component {
componentDidMount(){
this.hello = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
});
}
handlePress() {
this.hello.play((success) => {
if (!success) {
console.log('Sound did not play')
}
})
}
render() {
const { movie } = this.props
return (
<TouchableOpacity onPress={this.handlePress.bind(this)}>
<View>
<Text>Start</Text>
</View>
</TouchableOpacity>
)
}
}
回答2:
Thanks very much who has answer to this question, but i have resolve this with this simple one:
import React, { Component } from 'react'
import { Text, View, TouchableOpacity } from 'react-native'
import Sound from 'react-native-sound';
export default class MovieList extends Component {
sound = new Sound('motorcycle.mp3');
playSound = () => {
this.sound.play()
}
render() {
return (
<View>
<TouchableOpacity onPress={this.playSound}>
<View>
<Text>Start</Text>
</View>
</TouchableOpacity>
</View>
)
}
}
回答3:
Try this code for play sound:
setTimeout(() => {
var sound = new Sound("motorcycle.mp3",Sound.MAIN_BUNDLE, (error) => {
/* ... */
});
setTimeout(() => {
sound.play((success) => {
/* ... */
});
}, 100);
}, 100);
It's hacky and solved by https://github.com/zmxv/react-native-sound/issues/89#issuecomment-276678935
回答4:
For iOS what worked for me is the following:
- Checked that my resource file was being played in xCode
- Open xCode
- Select the file
- Click play in the xCode player
- Once it was playing sound (had issues with some files, possibly encoding),
- Add the file as resource (Make sure is in Build Phases)
- Compile the project in xCode and run it from there
- Note: Every time you change the resources or expect new resources you need to compile your Native application either by doing it from xCode or
npm run ios
- Note: Every time you change the resources or expect new resources you need to compile your Native application either by doing it from xCode or
This is my code:
const sound = new Sound(
"myFile.mp3",
Sound.MAIN_BUNDLE,
error => {
if (error) {
console.log("failed to load the sound", error);
return;
}
sound.play(() => sound.release());
}
);
// The play dispatcher
sound.play();
来源:https://stackoverflow.com/questions/53737196/how-to-play-sound-in-react-native