【发布时间】:2019-05-13 05:08:27
【问题描述】:
我想在 React Native 中播放声音。
我曾尝试阅读此处zmxv/react-native-sound,但作为像我这样的初学者,该文档让我很困惑如何在 React Native 代码中应用它。
在我尝试this one 之前,在事件上做出反应本机播放声音并制作如下代码:
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>
)
}
}
这是我放音频的地方:
MyProject/android/app/src/main/res/raw/motorcycle.mp3
项目结构
那么,我的代码有什么问题?
【问题讨论】:
-
我认为您是在加载之前播放声音。
标签: javascript android react-native react-native-sound