Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any audio puase/stop listener? #817

Open
Robiullah2244 opened this issue Jan 21, 2023 · 1 comment
Open

Is there any audio puase/stop listener? #817

Robiullah2244 opened this issue Jan 21, 2023 · 1 comment
Labels

Comments

@Robiullah2244
Copy link

I have to update my component if the audio is stopped from another component.

@sultson
Copy link

sultson commented Feb 24, 2023

Hi @Robiullah2244 - the library does not provide listeners, but you can accomplish this with useState() instead. Here's a small example of it, where the displayed text & button are dependent on whether audio is currently playing or not. Let me know if this fixes the issue for you!

App.tsx

import React, { useState } from 'react';
import { Text, View, Button, StyleSheet } from 'react-native';
import Sound from 'react-native-sound'

Sound.setCategory('Playback');
var whoosh = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
  if (error) {
    console.log('failed to load the sound', error);
    return;
  }
});


function App(): JSX.Element {
  const [playing, setPlaying] = useState(false)

  function toggleSound() {
    if (playing) {
      whoosh.stop(() => setPlaying(false))
    } else {
      setPlaying(true)
      whoosh.play((success) => {
        if (!success) { console.log('Playback failed due to decoding errors') }
        setPlaying(false)
      });
    }
    
  }

  return (
    <View style={styles.container}>
      <Text style={styles.text} >{playing ? 'Audio is currently playing' : 'Audio is stopped'}</Text>
      <Button title={playing ? 'stop' : 'play'} onPress={toggleSound} />
      
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent:'center',
    alignItems:'center'
  },
  text: {
    fontSize:20,
    margin:10
  }
})

export default App;

Environment

package version
react-native 0.71.3
react-native-sound 0.11.2

@BraveEvidence
Copy link

This will help https://www.youtube.com/watch?v=vVI7ZAZq5e0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants
@sultson @BraveEvidence @Robiullah2244 and others