【问题标题】:ReactJS - How do I play an audio element onKeyPress and onClick?ReactJS - 如何在 onKeyPress 和 onClick 上播放音频元素?
【发布时间】:2021-11-10 20:56:43
【问题描述】:

所以我正在尝试从 freeCodeCamp 构建 Drum Machine 项目,但我遇到了音频元素的这个问题。我写了下面的代码。我没有收到任何错误,但是当我单击带有“drum-pad”类的 div 时,音频仍然无法播放。我知道如何在没有 audio 元素的情况下完成这项工作,但挑战需要专门使用该元素。

我仍然会尝试自己解决这个问题,如果我在其他地方找到解决方案,我会更新这个问题。

这是我的 DrumPad 组件

import React from 'react';

class DrumPad extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            name: this.props.name,
            audio: this.props.source,
            indexNr: this.props.indexNr,
            keyId: this.props.keyId
        }
        this.clickHandle = this.clickHandle.bind(this);
        this.keyPressHandle = this.keyPressHandle.bind(this);
    }


    clickHandle() {
        document.getElementById(`audio${this.state.indexNr}`).play();
    }

    keyPressHandle(event) {
        if(event.key === this.state.keyId || event.key === this.state.keyId.toLowerCase()){
            document.getElementById(`audio${this.state.indexNr}`).play();
        }
    }

    render(){
        return (
            <div className="drum-pad" id={this.name} onClick={this.clickHandle} onKeyPress={this.keyPressHandle}>
                    <h3>{this.state.keyId}</h3>
                    <audio id={`audio${this.state.indexNr}`} src={this.audio} />
            </div>
        )
    }
}

export default DrumPad; 

这是我的键盘组件

import DrumPad from "./DrumpPad";

const Keyboard = (props) => {
    return <div className="column1">
        { 
            soundsArr.map((sound, index) => <DrumPad key={index} indexNr={index} name={sound.name} source={sound.source} keyId={sound.keyId} />) 
        }
    </div>
}

export default Keyboard;


const soundsArr = [
    {
      name: "Drum0", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "Q"
    },
    {
      name: "Drum1", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "W"
    },
    {
      name: "Drum2", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "E"
    },
    {
      name: "Drum3", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "A"
    },
    {
      name: "Drum4", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "S"
    },
    {
      name: "Drum5", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "D"
    },
    {
      name: "Drum6", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "Z"
    },
    {
      name: "Drum7", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "X"
    },
    {
      name: "Drum8", 
      source: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3",
      keyId: "C"
    }
  ];

【问题讨论】:

    标签: javascript reactjs html5-audio


    【解决方案1】:
    很多。但这是我编辑代码的方式。


    • DrumPad 组件中,您将所有道具添加到状态。这不是必需的,因为您没有更改任何值,因此最好将组件从 Class 转换为 Function 组件。
    • 您向DrumPad 组件中的父div 元素添加了一个onKeyPress 事件。那将永远不会运行,因为该元素无法被关注。因此,您应该使用useEffect 挂钩将事件附加到window 并播放音频。
    • 不建议使用数组的索引作为它的 key prop。您应该使用DrumPad 中的namekeyId,因为它已经是唯一的了。在Keyboard 组件的render 函数中。
    • 从上一点开始,您可以使用keyname 来生成音频的ID。这意味着不需要传递 indexNr 道具。或者您可以使用useRef 挂钩访问audio

    【讨论】:

    • 这对我来说是很多新信息,因为我以前从未使用过钩子,但我会研究你写的代码,在谷歌的帮助下我会理解的:D 感谢您的回复!
    • 好的。祝你好运。如果您还需要什么,请告诉我。我很乐意尽我所能提供帮助和解释。
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 2011-03-03
    • 2014-07-16
    • 2022-11-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多