Стоп звук в реактивном компоненте - PullRequest
0 голосов
/ 06 ноября 2018

Мое приложение воспроизводит некоторые аудио в зависимости от различных триггеров. Я хочу, чтобы пользователь мог остановить звук, когда он решит это.

это то, что я сделал, основываясь на этом посте :

  audioPlayer(sound){
      const audioGetReady = new Audio("...mp3")
      const audioTenSec = new Audio("...mp3");
      const audioNext = new AudioAudio("...mp3");
      const audioRest = new AudioAudio("...mp3")
      const audioCongrats = new AudioAudio("...mp3")

      if(sound == 'getReady'){
        audioGetReady.play()
      }
      if(sound == 'tenSeconds'){
        audioTenSec.play()
      }
      if(sound == 'next'){
        audioNext.play()
      }
      if(sound == 'rest'){
        audioRest.play()
      }
      if(sound == 'congrats'){
        audioCongrats.play()
      }
      if(sound == 'stop'){
        audioGetReady.pause();
        audioGetReady.currentTime = 0;
        audioTenSec.pause();
        audioTenSec.currentTime = 0;
        audioNext.pause();
        audioNext.currentTime = 0;
        audioRest.pause();
        audioRest.currentTime = 0;
        audioCongrats.pause();
        audioCongrats.currentTime = 0;
      }
    }

Это не работает, я также пытался использовать ".muted = true;" как показано здесь

1 Ответ

0 голосов
/ 06 ноября 2018

Я думаю, ваш код должен быть таким, как показано ниже. В вашем коде каждый раз, когда вы вызываете функцию audioPlayer, создается новый объект Audio, что означает, что звук, который вы запускаете и приостанавливаете, отличается.

  audioGetReady = new Audio("...mp3");
  audioTenSec = new Audio("...mp3");
  audioNext = new AudioAudio("...mp3");
  audioRest = new AudioAudio("...mp3");
  audioCongrats = new AudioAudio("...mp3");

  audioPlayer(sound){
    if(sound == 'getReady'){
      this.audioGetReady.play()
    }
    if(sound == 'tenSeconds'){
      this.audioTenSec.play()
    }
    if(sound == 'next'){
      this.audioNext.play()
    }
    if(sound == 'rest'){
      this.audioRest.play()
    }
    if(sound == 'congrats'){
      this.audioCongrats.play()
    }
    if(sound == 'stop'){
      this.audioGetReady.pause();
      this.audioGetReady.currentTime = 0;
      this.audioTenSec.pause();
      this.audioTenSec.currentTime = 0;
      this.audioNext.pause();
      this.audioNext.currentTime = 0;
      this.audioRest.pause();
      this.audioRest.currentTime = 0;
      this.audioCongrats.pause();
      this.audioCongrats.currentTime = 0;
    }
  }
...