react-native – 在事件中对原生播放声音做出反应

我有一个组件,如:
import React,{ Component } from 'react'
import { StyleSheet,Text,View,TouchableOpacity } from 'react-native'


class MovieList extends Component {


    handlePress() {
        // Play some sound here
    }

    render() {
        const { movie } = this.props
        return (
            <TouchableOpacity onPress={this.handlePress.bind(this)}>
                <View style={styles.movie}>
                    <Text style={styles.name}>{movie.name}</Text>
                    <View style={styles.start}>
                        <Text style={styles.text}>Start</Text>
                    </View>
                </View>
            </TouchableOpacity>
        )
    }
}

在这里,当我触摸视图时,我想播放一些声音.
搜索了它,但没有找到任何适当的答案

无论如何,当我按下某些东西时,我能发出声音吗?
我怎样才能做到这一点 ?

查看 React Native Sound – 用于访问设备音频控件的跨平台组件.

您可以像这样使用它:

const Sound = require('react-native-sound')

let hello = new Sound('hello.mp3',Sound.MAIN_BUNDLE,(error) => {
  if (error) {
    console.log(error)
  }
})

hello.play((success) => {
  if (!success) {
    console.log('Sound did not play')
  }
})

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...