Why is it that when I click play, I can play the sound and when I click play, there is no sound??
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;
Howler. js offers a consistent API for using audio with JavaScript, with control over common audio patterns including play, pause, seek to rate, fade and loop. Audio files are automatically cached where possible to improve playback performance.
It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.
I recently published react-use-audio-player which is a react hooks abstraction of howlerjs. You can use it like so:
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>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With