Created
July 10, 2023 10:30
-
-
Save Thisisjuke/e9d156c1b5fddddbb8983d43da55e529 to your computer and use it in GitHub Desktop.
Simple useAudioPlayer hook 🎵 to create a Music player from a Blob ⏯️
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef, useState } from 'react' | |
function useAudioPlayer() { | |
const audioContextRef = useRef<AudioContext | null>(null) | |
const sourceRef = useRef<AudioBufferSourceNode | null>(null) | |
const [isPlaying, setIsPlaying] = useState<boolean>(false) | |
const [audioBlob, setAudioBlob] = useState<Blob | null>(null) | |
const [totalTime, setTotalTime] = useState<number>(0) | |
const createAudioSource = (): void => { | |
if (audioBlob && audioContextRef.current) { | |
const fileReader = new FileReader() | |
fileReader.onload = (): void => { | |
audioContextRef.current!.decodeAudioData( | |
fileReader.result as ArrayBuffer, | |
(buffer: AudioBuffer) => { | |
sourceRef.current = audioContextRef.current!.createBufferSource() | |
sourceRef.current.buffer = buffer | |
sourceRef.current.connect(audioContextRef.current!.destination) | |
sourceRef.current.onended = (): void => { | |
setIsPlaying(false) | |
} | |
setTotalTime(buffer.duration) | |
sourceRef.current.start() | |
}, | |
) | |
} | |
fileReader.readAsArrayBuffer(audioBlob) | |
} | |
} | |
const playAudio = (): void => { | |
if (!isPlaying) { | |
if (sourceRef.current && audioContextRef.current?.state === 'suspended') { | |
audioContextRef.current.resume().then(() => { | |
setIsPlaying(true) | |
}) | |
} | |
else { | |
if (audioContextRef.current?.state === 'suspended') { | |
audioContextRef.current.resume().then(() => { | |
createAudioSource() | |
setIsPlaying(true) | |
}) | |
} | |
else { | |
audioContextRef.current = new AudioContext() | |
createAudioSource() | |
setIsPlaying(true) | |
} | |
} | |
} | |
} | |
const pauseAudio = (): void => { | |
if (isPlaying && sourceRef.current) { | |
sourceRef.current.stop() | |
setIsPlaying(false) | |
} | |
} | |
const reset = (): void => { | |
if (sourceRef.current) { | |
sourceRef.current.stop() | |
sourceRef.current.disconnect() | |
sourceRef.current = null | |
} | |
if (audioContextRef.current) { | |
audioContextRef.current.close() | |
audioContextRef.current = null | |
} | |
setIsPlaying(false) | |
} | |
const setAudio = (audio: Blob | null): void => { | |
setAudioBlob(audio) | |
} | |
const getTotalTime = (): number => { | |
return totalTime | |
} | |
useEffect(() => { | |
if (!audioContextRef.current) { | |
audioContextRef.current = new AudioContext() | |
} | |
return reset | |
}, []) | |
return { playAudio, pauseAudio, isPlaying, setAudio, reset, getTotalTime } | |
} | |
export default useAudioPlayer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React Audio Player Hook
This is a React hook that enables audio playback from a Blob. It provides functions to play, pause, set audio, and retrieve the total duration of the audio.
Installation
Usage
Example:
How to send this file to my backend ?
You should use a Form Data that you will include in your Payload Body.
Dependencies
This hook requires React to be installed as a dependency in your project.
License
This code is provided under the MIT License. Feel free to modify and use it in your projects.