在事件上反应原生播放声音 [英] react native play sound on event
本文介绍了在事件上反应原生播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的组件:
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
时,我想播放一些声音.我用谷歌搜索过,但没有找到任何合适的答案
Here when I touch the view
I want to play some sound.
I have googled about it but not found any appropriate answer
无论如何我可以在按下某个东西时播放声音吗?我该怎么做?
Is there anyway I can play sound when I press to something? How can I do this ?
推荐答案
查看 React Native声音 - 用于访问设备音频控件的跨平台组件.
Check out React Native Sound - a cross platform component for accessing device audio controls.
你可以这样使用它:
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')
}
})
这篇关于在事件上反应原生播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文