react-native - 在事件上 react 原生播放声音

标签 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>
        )
    }
}

在这里,当我触摸 view我想播放一些声音。
我用谷歌搜索过,但没有找到任何合适的答案

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

最佳答案

退房 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-native - 在事件上 react 原生播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536960/

相关文章:

reactjs - 如何删除抽屉导航上的阴影覆盖?

react-native - 如何解决/修复 React Native 编译错误 : AAPT: No resource identifier found for attribute 'appComponentFactory' in package 'android' ?

javascript - react native Android 生成签名 APK 给出资源错误

typescript - 我的react-native expo 应用程序的已编译 typescript js 文件在哪里?

javascript - 有没有一种方法可以判断它是 native react 还是网络 react ?

android - React-Native 通过教程 Facebook 获取 JSON 返回错误

json - 如何在 React Native 中从存储在 GitHub 上的 JSON 文件加载数据?

android - 设置 firebase 时出错, react 可能是未处理的 promise 拒绝

android - 使用 React-Native-Router-Flux 在所有屏幕中显示组件 - React Native

react-native - 在 TimePickerAndroid react native 中设置最短时间