2016-05-31 17 views
6

Ho un componente simile:reagire suono gioco nativo su evento

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

Qui quando tocco la view voglio giocare qualche suono. Ho cercato su google ma non ho trovato nessuna risposta appropriata

C'è comunque la possibilità di riprodurre il suono quando premo qualcosa? Come posso fare questo?

+0

Date un'occhiata a https://github.com/zmxv/react-native-sound lo fa esattamente ciò che serve. –

risposta

7

Check out React Native Sound - un componente multipiattaforma per accedere ai controlli audio del dispositivo.

Si può usare in questo modo:

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

Qualsiasi esempio di utilizzo di questa libreria 'react-native-sound' ma con url mp3 link? – jose920405

+2

Potrebbe essere necessario richiedere anche l'audio, altrimenti è probabile che si verifichi un errore. https://github.com/zmxv/react-native-sound/issues/120 – fodma1

+0

Come riprodurre il suono sul pulsante? puoi mostrare qualche esempio –

2

Si può provare componente audio da expo-sdk.

Verificare un esempio here.

Funziona con sdk 18.

Problemi correlati