zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.78k stars 747 forks source link

Sound doesn't stop playing if others are playing. #797

Open AhmedElwerdany opened 1 year ago

AhmedElwerdany commented 1 year ago

:beetle: Description

for example, I have two audios. (audio_1) and (audio_2). I play (audio_1) , then I play (audio_2). but they work with each other. I want the (audio_1) to stop if I played (audio_2), and vice versa

:beetle: What have you tried?

each audio is initiated with "Sound" Class.

:beetle: Please post your code:

const Audio = ({ duration, vis, type, answerComponents: { url } }) => {
  const [isPlaying, setIsPlaying] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [sound, setSound] = useState(undefined);
  const [soundCount, setSoundCount] = useState(0);

  const [pressed, setPressed] = useState(false);

  const onPress = () => {
    setPressed(true);
    if (!sound) {
      setIsLoading(true);
      const s = new Sound(url, Sound.MAIN_BUNDLE, (err) => {
        Sound.setActive(true);
        setIsLoading(false);
        if (err) {
          return;
        }
        s.play((success) => {
          s.release();
        });
      });
      setSound(s);
    } else {
      if (sound.isPlaying()) {
        sound.pause(() => {
          setIsPlaying(false);
        });
      } else {
        sound.play();
      }
    }
  };

:bulb: Possible solution

Is your issue with...

Are you using...

Which versions are you using?

Does the problem occur on...

If your problem is happening on a device, which device?

shubhammalvedev commented 1 year ago

you can use: whoosh.stop(() => { // Note: If you want to play a sound after stopping and rewinding it, // it is important to call play() in a callback. // whoosh.play(); }); you need to stop first audio before playing second audio