audio load
Signed-off-by: ale <ale@manalejandro.com>
This commit is contained in:
parent
92bef3e97b
commit
2076561a4d
12
src/App.js
12
src/App.js
@ -1,5 +1,5 @@
|
|||||||
import "./App.css";
|
import "./App.css";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import WaveForm from "./WaveForm";
|
import WaveForm from "./WaveForm";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
@ -16,11 +16,11 @@ const App = () => {
|
|||||||
audioElmRef = useRef(null),
|
audioElmRef = useRef(null),
|
||||||
audioAnalyzer = () => {
|
audioAnalyzer = () => {
|
||||||
const audioCtx = new (window.AudioContext || window.webkitAudioContext)(),
|
const audioCtx = new (window.AudioContext || window.webkitAudioContext)(),
|
||||||
|
source = audioCtx.createMediaElementSource(audioElmRef.current),
|
||||||
analyzer = audioCtx.createAnalyser()
|
analyzer = audioCtx.createAnalyser()
|
||||||
analyzer.fftSize = 2048
|
analyzer.fftSize = 2048
|
||||||
const bufferLength = analyzer.frequencyBinCount,
|
const bufferLength = analyzer.frequencyBinCount,
|
||||||
dataArray = new Uint8Array(bufferLength),
|
dataArray = new Uint8Array(bufferLength)
|
||||||
source = audioCtx.createMediaElementSource(audioElmRef.current)
|
|
||||||
source.connect(analyzer)
|
source.connect(analyzer)
|
||||||
source.connect(audioCtx.destination)
|
source.connect(audioCtx.destination)
|
||||||
source.onended = () => {
|
source.onended = () => {
|
||||||
@ -102,10 +102,10 @@ const App = () => {
|
|||||||
<div>
|
<div>
|
||||||
<h2>Audio Controls</h2>
|
<h2>Audio Controls</h2>
|
||||||
{audioElmRef.current?.paused ?
|
{audioElmRef.current?.paused ?
|
||||||
<button onClick={() => {
|
<button onClick={() => useCallback(async () => {
|
||||||
setMuted(false)
|
setMuted(false)
|
||||||
audioElmRef.current?.play()
|
await audioElmRef.current?.play()
|
||||||
}}>Play ⏯️</button> :
|
}, [audioElmRef.current])}>Play ⏯️</button> :
|
||||||
<button onClick={() => audioElmRef.current?.pause()}>Pause ⏯️</button>
|
<button onClick={() => audioElmRef.current?.pause()}>Pause ⏯️</button>
|
||||||
}
|
}
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user