亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當在 react js 上觸發 onClick 方法時,有什么方法可以在特定時間間隔內播放聲音

當在 react js 上觸發 onClick 方法時,有什么方法可以在特定時間間隔內播放聲音

喵喵時光機 2023-06-09 14:57:30
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 秒的嘎嘎聲!
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

我會這樣做:


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



class MallardDuck extends Duck {

  constructor(props) {

    super();

    this.state = {

      canFly: false,

      canQuack: false,

      round:0,

      quackSound: new Audio(quackSound),

    };

    this.intervalRef=null;

  }


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

            //you can tweek interval duration for your liking , for now its set to pay every .3 seconds (300 ms) 

           const myInterval=setInterval(()=>{

              if(this.state.round > 3) clearInterval(myInterval);

             this.setState({ canQuack: true });

             this.setState({ round:  this.state.round+1 });

             this.state.quackSound.play(); // Here !!!!!!},300)

          }}

        >

          Quack

        </Button>

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

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

      </div>

    );

  }

}


查看完整回答
反對 回復 2023-06-09
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號