2016-05-31 56 views
6

我有这样一个组成部分:反应本地播放声音事件

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我要玩一些声音。 我用Google搜索了一下,但没有找到任何合适的答案

有没有反正我可以播放声音时,我按下的东西? 我该怎么做?

+0

看看https://github.com/zmxv/react-native-sound它正是你需要的。 –

回答

7

检出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') 
    } 
}) 
+0

使用此库'react-native-sound'但使用url mp3链接的任何示例? – jose920405

+2

您可能还想要声音,否则可能会出错。 https://github.com/zmxv/react-native-sound/issues/120 – fodma1

+0

如何播放按钮按下声音?你能举一些例子吗? –