zmxv / react-native-sound

React Native module for playing sound clips

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sound doesn't stop playing if others are playing.

AhmedElwerdany opened this issue · comments

🪲 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

🪲 What have you tried?

each audio is initiated with "Sound" Class.

🪲 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();
      }
    }
  };

💡 Possible solution

Is your issue with...

  • iOS
  • Android
  • Windows

Are you using...

  • React Native CLI (e.g. react-native run-android)
  • Expo
  • Other: (please specify)

Which versions are you using?

  • React Native Sound: 0.11.2
  • React Native: 0.68.2
  • iOS:
  • Android: 8.1.0
  • Windows:

Does the problem occur on...

  • Simulator
  • Device

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

  • Device: Honor 8C

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