11// with thanks to https://medium.com/front-end-weekly/recording-audio-in-mp3-using-reactjs-under-5-minutes-5e960defaf10
22
33import MicRecorder from 'mic-recorder-to-mp3' ;
4- import { useEffect , useRef , useState } from 'react' ;
4+ import { useEffect , useRef , useState , useCallback } from 'react' ;
55import Button from 'react-bootstrap/Button' ;
66import {
77 FaMicrophone ,
@@ -33,9 +33,58 @@ function AudioViewer({ src }) {
3333 const containerW = useRef ( null ) ;
3434 const waveSurf = useRef ( null ) ;
3535 const volume = useRef ( null ) ;
36+ let vMute ;
37+ let vOff ;
38+ let vDown ;
39+ let vUp ;
3640 const play = < FaPlay style = { { paddingLeft : '2px' } } /> ;
3741 const pause = < FaPause /> ;
38- const vMute = (
42+ const [ playing , setPlay ] = useState ( play ) ;
43+ const [ volumeIndex , changeVolume ] = useState ( null ) ;
44+
45+ const toggleVolume = useCallback ( ( ) => {
46+ if ( volume . current ) {
47+ const volumeValue = parseFloat ( volume . current . value ) ;
48+ if ( volumeValue !== 0 ) {
49+ volume . current . value = 0 ;
50+ waveSurf . current . setVolume ( volume . current . value ) ;
51+ volume . current . style . setProperty ( '--volumePercent' , `${ 0 } %` ) ;
52+ changeVolume ( vMute ) ;
53+ } else {
54+ volume . current . value = 1 ;
55+ waveSurf . current . setVolume ( volume . current . value ) ;
56+ volume . current . style . setProperty ( '--volumePercent' , `${ 100 } %` ) ;
57+ changeVolume ( vUp ) ;
58+ }
59+ }
60+ } , [ ] ) ;
61+
62+ const playPause = useCallback ( ( ) => {
63+ if ( waveSurf . current . isPlaying ( ) ) {
64+ setPlay ( play ) ;
65+ waveSurf . current . pause ( ) ;
66+ } else {
67+ setPlay ( pause ) ;
68+ waveSurf . current . play ( ) ;
69+ }
70+ } , [ ] ) ;
71+
72+ function handleVolumeChange ( ) {
73+ waveSurf . current . setVolume ( volume . current . value ) ;
74+ const volumeNum = volume . current . value * 100 ;
75+ volume . current . style . setProperty ( '--volumePercent' , `${ volumeNum } %` ) ;
76+ if ( volume . current . value === 0 ) {
77+ changeVolume ( vMute ) ;
78+ } else if ( volume . current . value < 0.25 ) {
79+ changeVolume ( vOff ) ;
80+ } else if ( volume . current . value < 0.5 ) {
81+ changeVolume ( vDown ) ;
82+ } else if ( volume . current . value < 0.75 ) {
83+ changeVolume ( vUp ) ;
84+ }
85+ }
86+
87+ vMute = (
3988 < FaVolumeMute
4089 style = { {
4190 width : '1.05em' ,
@@ -47,19 +96,19 @@ function AudioViewer({ src }) {
4796 onClick = { toggleVolume }
4897 />
4998 ) ;
50- const vOff = (
99+ vOff = (
51100 < FaVolumeOff
52101 style = { { cursor : 'pointer' , paddingRight : '9px' } }
53102 onClick = { toggleVolume }
54103 />
55104 ) ;
56- const vDown = (
105+ vDown = (
57106 < FaVolumeDown
58107 style = { { cursor : 'pointer' , paddingRight : '3px' } }
59108 onClick = { toggleVolume }
60109 />
61110 ) ;
62- const vUp = (
111+ vUp = (
63112 < FaVolumeUp
64113 style = { {
65114 width : '1.23em' ,
@@ -70,10 +119,9 @@ function AudioViewer({ src }) {
70119 onClick = { toggleVolume }
71120 />
72121 ) ;
73- const [ playing , setPlay ] = useState ( play ) ;
74- const [ volumeIndex , changeVolume ] = useState ( vUp ) ;
75122
76123 useEffect ( ( ) => {
124+ changeVolume ( vUp ) ;
77125 if ( containerW . current && ! waveSurf . current ) {
78126 waveSurf . current = WaveSurfer . create ( {
79127 container : containerW . current ,
@@ -101,46 +149,6 @@ function AudioViewer({ src }) {
101149 }
102150 } , [ ] ) ;
103151
104- function handleVolumeChange ( ) {
105- waveSurf . current . setVolume ( volume . current . value ) ;
106- const volumeNum = volume . current . value * 100 ;
107- volume . current . style . setProperty ( '--volumePercent' , `${ volumeNum } %` ) ;
108- if ( volume . current . value === 0 ) {
109- changeVolume ( vMute ) ;
110- } else if ( volume . current . value < 0.25 ) {
111- changeVolume ( vOff ) ;
112- } else if ( volume . current . value < 0.5 ) {
113- changeVolume ( vDown ) ;
114- } else if ( volume . current . value < 0.75 ) {
115- changeVolume ( vUp ) ;
116- }
117- }
118-
119- function toggleVolume ( ) {
120- if ( volume . current ) {
121- if ( volume . current . value !== 0 ) {
122- volume . current . value = 0 ;
123- waveSurf . current . setVolume ( volume . current . value ) ;
124- volume . current . style . setProperty ( '--volumePercent' , `${ 0 } %` ) ;
125- changeVolume ( vMute ) ;
126- } else {
127- volume . current . value = 1 ;
128- waveSurf . current . setVolume ( volume . current . value ) ;
129- volume . current . style . setProperty ( '--volumePercent' , `${ 100 } %` ) ;
130- changeVolume ( vUp ) ;
131- }
132- }
133- }
134-
135- function playPause ( ) {
136- if ( waveSurf . current . isPlaying ( ) ) {
137- setPlay ( play ) ;
138- waveSurf . current . pause ( ) ;
139- } else {
140- setPlay ( pause ) ;
141- waveSurf . current . play ( ) ;
142- }
143- }
144152 if ( waveSurf . current ) {
145153 waveSurf . current . on ( 'finish' , ( ) => {
146154 setPlay ( play ) ;
0 commit comments