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

前端之家收集整理的这篇文章主要介绍了react-native – 在事件中对原生播放声音做出反应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个组件,如:
  1. import React,{ Component } from 'react'
  2. import { StyleSheet,Text,View,TouchableOpacity } from 'react-native'
  3.  
  4.  
  5. class MovieList extends Component {
  6.  
  7.  
  8. handlePress() {
  9. // Play some sound here
  10. }
  11.  
  12. render() {
  13. const { movie } = this.props
  14. return (
  15. <TouchableOpacity onPress={this.handlePress.bind(this)}>
  16. <View style={styles.movie}>
  17. <Text style={styles.name}>{movie.name}</Text>
  18. <View style={styles.start}>
  19. <Text style={styles.text}>Start</Text>
  20. </View>
  21. </View>
  22. </TouchableOpacity>
  23. )
  24. }
  25. }

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

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

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

您可以像这样使用它:

  1. const Sound = require('react-native-sound')
  2.  
  3. let hello = new Sound('hello.mp3',Sound.MAIN_BUNDLE,(error) => {
  4. if (error) {
  5. console.log(error)
  6. }
  7. })
  8.  
  9. hello.play((success) => {
  10. if (!success) {
  11. console.log('Sound did not play')
  12. }
  13. })

猜你在找的React相关文章