playPause
Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
parent
c233fcc017
commit
7b43c3b428
23
src/App.js
23
src/App.js
@ -13,6 +13,7 @@ const App = () => {
|
|||||||
[title, setTitle] = useState('Stream Radio'),
|
[title, setTitle] = useState('Stream Radio'),
|
||||||
[currentListeners, setCurrentListeners] = useState(0),
|
[currentListeners, setCurrentListeners] = useState(0),
|
||||||
[maxListeners, setMaxListeners] = useState(0),
|
[maxListeners, setMaxListeners] = useState(0),
|
||||||
|
[playPause, setPlayPause] = useState(false),
|
||||||
audioElmRef = useRef(null),
|
audioElmRef = useRef(null),
|
||||||
once = useRef(false),
|
once = useRef(false),
|
||||||
audioAnalyzer = () => {
|
audioAnalyzer = () => {
|
||||||
@ -29,16 +30,24 @@ const App = () => {
|
|||||||
}
|
}
|
||||||
setAnalyzerData({ analyzer, bufferLength, dataArray })
|
setAnalyzerData({ analyzer, bufferLength, dataArray })
|
||||||
},
|
},
|
||||||
play = useCallback(async () => {
|
play = () => {
|
||||||
if (audioElmRef.current) {
|
if (audioElmRef.current) {
|
||||||
await audioElmRef.current.play()
|
audioElmRef.current.play().then(() => {
|
||||||
|
setPlayPause(true)
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Error playing audio: ' + err.message)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, [audioElmRef]),
|
},
|
||||||
pause = useCallback(async () => {
|
pause = () => {
|
||||||
if (audioElmRef.current) {
|
if (audioElmRef.current) {
|
||||||
await audioElmRef.current.pause()
|
audioElmRef.current.pause().then(() => {
|
||||||
|
setPlayPause(false)
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Error pausing audio: ' + err.message)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, [audioElmRef]),
|
},
|
||||||
loadData = () => {
|
loadData = () => {
|
||||||
fetch('/stream.json').then(response => response.json()).then(json => {
|
fetch('/stream.json').then(response => response.json()).then(json => {
|
||||||
setJson(json)
|
setJson(json)
|
||||||
@ -112,7 +121,7 @@ const App = () => {
|
|||||||
<h4><p className="bounce"><a href={link} target="_blank" title={title} alt={title}>{bounce}</a></p></h4>
|
<h4><p className="bounce"><a href={link} target="_blank" title={title} alt={title}>{bounce}</a></p></h4>
|
||||||
<div>
|
<div>
|
||||||
<h2>Audio Controls</h2>
|
<h2>Audio Controls</h2>
|
||||||
{audioElmRef.current?.paused ?
|
{!playPause ?
|
||||||
<button onClick={play}>Play ⏯️</button> :
|
<button onClick={play}>Play ⏯️</button> :
|
||||||
<button onClick={pause}>Pause ⏯️</button>
|
<button onClick={pause}>Pause ⏯️</button>
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user