2016-05-31 102 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

如何播放按鈕按下聲音?你能舉一些例子嗎? –