当在 react js 上触发 onClick 方法时,有什么方法可以在特定时间间隔内播放声音

import quackSound from "./music/Duck-quack.mp3";



class MallardDuck extends Duck {

  constructor(props) {

    super();

    this.state = {

      canFly: false,

      canQuack: false,

      quackSound: new Audio(quackSound),

    };

  }


  quack = () => {

    const quack = new QuackSound();

    return quack.quack();

  };


  fly = () => {

    const fly = new FlyWings();

    return fly.fly();

  };


  render() {

    return (

      <div className="duck">

        <img

          className={`image ${this.state.canFly ? "canFly" : ""}`}

          src={mallardDuck}

          alt="mallardDuck"

          onAnimationEnd={() => {

            this.setState({ canFly: false });

          }}

        />

        <Button

          className="eventButton"

          onClick={(event) => {

            event.preventDefault();

            this.setState({ canFly: true });

          }}

        >

          Fly

        </Button>

        <Button

          className="eventButton"

          onClick={(event) => {

            event.preventDefault();

            this.setState({ canQuack: true });

            this.state.quackSound.play(); // Here !!!!!!

          }}

        >

          Quack

        </Button>

        {this.state.canQuack ? this.quack() : null}

        {this.state.canFly ? this.fly() : null}

      </div>

    );

  }

}


我的音频 mp3 文件长 18 秒。我想玩前 3 或 4 秒。有什么办法可以在 react js 中做到这一点!上面提到的代码播放了整整18秒,我只想播放前几秒。我可以在 react js 中这样做吗?另外,我可以选择我的声音从哪里开始和结束吗?例如,如果我想播放 0.03 到 0.07 秒的嘎嘎声!


喵喵时光机
浏览 92回答 1
1回答

开心每一天1111

我会这样做:import quackSound from "./music/Duck-quack.mp3";class MallardDuck extends Duck {&nbsp; constructor(props) {&nbsp; &nbsp; super();&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; canFly: false,&nbsp; &nbsp; &nbsp; canQuack: false,&nbsp; &nbsp; &nbsp; round:0,&nbsp; &nbsp; &nbsp; quackSound: new Audio(quackSound),&nbsp; &nbsp; };&nbsp; &nbsp; this.intervalRef=null;&nbsp; }&nbsp; quack = () => {&nbsp; &nbsp; const quack = new QuackSound();&nbsp; &nbsp; return quack.quack();&nbsp; };&nbsp; fly = () => {&nbsp; &nbsp; const fly = new FlyWings();&nbsp; &nbsp; return fly.fly();&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div className="duck">&nbsp; &nbsp; &nbsp; &nbsp; <img&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; className={`image ${this.state.canFly ? "canFly" : ""}`}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src={mallardDuck}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alt="mallardDuck"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onAnimationEnd={() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setState({ canFly: false });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; className="eventButton"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick={(event) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setState({ canFly: true });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fly&nbsp; &nbsp; &nbsp; &nbsp; </Button>&nbsp; &nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; className="eventButton"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onClick={(event) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //you can tweek interval duration for your liking , for now its set to pay every .3 seconds (300 ms)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const myInterval=setInterval(()=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.state.round > 3) clearInterval(myInterval);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setState({ canQuack: true });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setState({ round:&nbsp; this.state.round+1 });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.state.quackSound.play(); // Here !!!!!!},300)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quack&nbsp; &nbsp; &nbsp; &nbsp; </Button>&nbsp; &nbsp; &nbsp; &nbsp; {this.state.canQuack ? this.quack() : null}&nbsp; &nbsp; &nbsp; &nbsp; {this.state.canFly ? this.fly() : null}&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript