Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native play sound on event

Tags:

react-native

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 ?

like image 261
gamer Avatar asked May 31 '16 04:05

gamer


People also ask

How do I turn off sound in react native?

To stop the soundconsole. log('Stop'); });


2 Answers

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')   } }) 
like image 168
Tom Walters Avatar answered Sep 23 '22 23:09

Tom Walters


You can try Audio component from expo-sdk.

Check an example here.

It works with sdk 18.

like image 37
Niakrais Avatar answered Sep 20 '22 23:09

Niakrais