我是 react-native 的新手,并尝试创建一个音频播放应用程序。
我使用反应原生声音来实现同样的目的。在文档中,它指定我可以从网络播放文件。但是我找不到任何相同的文档。
现在我正在从我的 ROR 后端上传音频文件,并将文件添加到 react 内的本地文件夹中。我正在将其更改为 aws s3。
音频文件的启动方式是这样的——
var whoosh = new Sound(this.state.soundFile, Sound.MAIN_BUNDLE, (error) => {
其中this.state.soundFile
是指定文件夹中的本地文件名(字符串(。
这可能吗?
您可以通过指定 url 而不是设置捆绑包来播放带有 react-native-sound 的远程声音:
import React, { Component } from 'react'
import { Button } from 'react-native'
import Sound from 'react-native-sound'
class RemoteSound extends Component {
playTrack = () => {
const track = new Sound('https://www.soundjay.com/button/button-1.mp3', null, (e) => {
if (e) {
console.log('error loading track:', e)
} else {
track.play()
}
})
}
render() {
return <Button title="play me" onPress={this.playTrack} />
}
}
export default RemoteSound
import Sound from 'react-native-sound';
const sound = new Sound('http://sounds.com/some-sound', null, (error) => {
if (error) {
// do something
}
// play when loaded
sound.play();
});