Hola, hallamos la respuesta a lo que estabas buscando, desplázate y la verás aquí.
Ejemplo 1: reproducir sonido onload reaccionar
importReact, useState, useEffect from"react";constuseAudio=url=>const[audio]=useState(newAudio(url));const[playing, setPlaying]=useState(false);consttoggle=()=>setPlaying(!playing);useEffect(()=>
playing ? audio.play(): audio.pause();,[playing]);useEffect(()=>
audio.addEventListener('ended',()=>setPlaying(false));return()=>
audio.removeEventListener('ended',()=>setPlaying(false));;,[]);return[playing, toggle];;constPlayer=( url )=>const[playing, toggle]=useAudio(url);return(<div><button onClick=toggle>playing ?"Pause":"Play"</button></div>);;exportdefaultPlayer;
Ejemplo 2: reproducir sonido onload reaccionar
classMusicextendsReact.Component
state =
play:false
audio =newAudio(this.props.url)componentDidMount()
audio.addEventListener('ended',()=>this.setState( play:false));componentWillUnmount()
audio.removeEventListener('ended',()=>this.setState( play:false));togglePlay=()=>this.setState( play:!this.state.play,()=>this.state.play?this.audio.play():this.audio.pause(););render()return(<div><button onClick=this.togglePlay>this.state.play?'Pause':'Play'</button></div>);exportdefaultMusic;
Te invitamos a añadir valor a nuestro contenido informacional cooperando tu experiencia en las reseñas.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)