我正在尝试将我制作的 HTML/Javascript 鼓包添加到我的 Gatsby 站点。当前,按下键时不会播放声音,并且从 audio.play() 返回的承诺挂在“待定”处。
我已经让它与基本的 HTML/JS 以及标准的 React 应用程序一起使用。但是当试图将它合并到我的 Gatsby 站点时,我遇到了以下返回的承诺问题,并且没有声音播放。
[[PromiseStatus]]: "pending"
[[PromiseValue]]: undefined
整个 Gatsby 站点的 git repo 在这里(目前只有开发版本)https://github.com/mfuller22/FullerStackProgramming
我遇到问题的特定页面的目录在这里https://github.com/mfuller22/FullerStackProgramming/tree/master/src/pages/Projects/DrumKit
我将在几个小时内添加我的 React 代码。我目前没有它。
componentDidMount() {
document.addEventListener("keydown", this.handleKeyDown.bind(this));
}
handleKeyDown = (event) => {
if (event.keyCode === parseInt(this.props.id, 10))
this.playSound()
}
playSound = () => {
const audio = document.getElementById('sound'+this.props.id);
const key = document.querySelector(`div[data-key="${this.props.id}"]`);
audio.currentTime = 0;
console.log(audio)
var playPromise = audio.play();
console.log(playPromise)
playPromise.catch(error => {
audio.play();
});
key.classList.add('playing');
};
render() {
const { id, letter, sound } = this.props
return (
<div
id={'key'+id}
data-key={id}
onClick={() => this._playSound}
onKeyDown={() => this._playSound}
onTransitionEnd={() => this._onTransitionEnd}
className="key">
<kbd>{letter}</kbd>
<span className="sound">{sound}</span>
<audio id={'sound'+id}>
<source src={sound} type="audio/wav" />
</audio>
</div>
);
}
我希望以已解决状态返回承诺,但目前承诺状态为待定,并且承诺值未定义。
慕无忌1623718
相关分类