gpt4 book ai didi

javascript - 如何在 react 中使用 howler.js?

转载 作者:行者123 更新时间:2023-11-30 06:18:11 30 4
gpt4 key购买 nike

为什么点播放有声音,点播放没有声音??

import React, { Component } from "react";
import { Howl, Howler } from 'howler';

class App extends Component {
SoundPlay() {
const Sounds = new Howl({
src: ["sound.mp3"]
})
Sounds.play()
console.log("sound")
}
render() {
return (
<div className="App">
<button onClick={this.SoundPlay}>play</button>
</div>
);
}
}

export default App;

最佳答案

我最近发布了 react-use-audio-player这是 howlerjs 的 react Hook 抽象。你可以像这样使用它:

import { useAudioPlayer } from "react-use-audio-player"

function MyComponent() {
const { play, pause, stop, playing } = useAudioPlayer()

const handlePlayPause = () => {
if (playing) {
pause()
} else {
play()
}
}

return (
<div className="audioControls">
<button onClick={handlePlayPause}>
{playing ? "pause" : "play"}
</button>
<button onClick={stop}>stop</button>
</div>
)
}

关于javascript - 如何在 react 中使用 howler.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54886538/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com