I have a component like:
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> ) } }
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 ?
To stop the soundconsole. log('Stop'); });
Check out React Native Sound - a cross platform component for accessing device audio controls.
You can use it like so:
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') } })
You can try Audio component from expo-sdk.
Check an example here.
It works with sdk 18.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With