zmxv / react-native-sound

React Native module for playing sound clips
MIT License
2.79k stars 748 forks source link

react-native-sound won't stop Android #603

Closed VadimRydenko closed 5 years ago

VadimRydenko commented 5 years ago
const soundItems = [{
  name: 'isound1',
  sound: 'computer_interface_signal.mp3',
},
{
  name: 'sound2',
  sound: 'smoke_trap_signal.mp3',
},
{
  name: 'sound3',
  sound: 'fire_signal.mp3',
},
{
  name: 'sound4',
  sound: 'submarine_alarm.mp3',
},
];

  playSound = (mySound) => {
    this.stopSound();
    const sound = new Sound(mySound, Sound.MAIN_BUNDLE, () => {
      sound.play();
    });
  }

  stopSound = () => {
    soundItems.forEach(soundItem => { 
      const sound = new Sound(soundItem.sound, Sound.MAIN_BUNDLE, () => {
        sound.stop();
      });
    });
  }
lmarques6 commented 5 years ago

You're initializing a new Sound in stopSound function instead of referencing the existing Sound object that is currently playing.

In playSound, store a reference to your Sound object, then use that reference in stopSound to stop it.