1
1
// with thanks to https://medium.com/front-end-weekly/recording-audio-in-mp3-using-reactjs-under-5-minutes-5e960defaf10
2
2
3
3
import MicRecorder from 'mic-recorder-to-mp3' ;
4
- import { useEffect , useRef , useState } from 'react' ;
4
+ import { useEffect , useRef , useState , useCallback } from 'react' ;
5
5
import Button from 'react-bootstrap/Button' ;
6
6
import {
7
7
FaMicrophone ,
@@ -33,9 +33,58 @@ function AudioViewer({ src }) {
33
33
const containerW = useRef ( null ) ;
34
34
const waveSurf = useRef ( null ) ;
35
35
const volume = useRef ( null ) ;
36
+ let vMute ;
37
+ let vOff ;
38
+ let vDown ;
39
+ let vUp ;
36
40
const play = < FaPlay style = { { paddingLeft : '2px' } } /> ;
37
41
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 = (
39
88
< FaVolumeMute
40
89
style = { {
41
90
width : '1.05em' ,
@@ -47,19 +96,19 @@ function AudioViewer({ src }) {
47
96
onClick = { toggleVolume }
48
97
/>
49
98
) ;
50
- const vOff = (
99
+ vOff = (
51
100
< FaVolumeOff
52
101
style = { { cursor : 'pointer' , paddingRight : '9px' } }
53
102
onClick = { toggleVolume }
54
103
/>
55
104
) ;
56
- const vDown = (
105
+ vDown = (
57
106
< FaVolumeDown
58
107
style = { { cursor : 'pointer' , paddingRight : '3px' } }
59
108
onClick = { toggleVolume }
60
109
/>
61
110
) ;
62
- const vUp = (
111
+ vUp = (
63
112
< FaVolumeUp
64
113
style = { {
65
114
width : '1.23em' ,
@@ -70,10 +119,9 @@ function AudioViewer({ src }) {
70
119
onClick = { toggleVolume }
71
120
/>
72
121
) ;
73
- const [ playing , setPlay ] = useState ( play ) ;
74
- const [ volumeIndex , changeVolume ] = useState ( vUp ) ;
75
122
76
123
useEffect ( ( ) => {
124
+ changeVolume ( vUp ) ;
77
125
if ( containerW . current && ! waveSurf . current ) {
78
126
waveSurf . current = WaveSurfer . create ( {
79
127
container : containerW . current ,
@@ -101,46 +149,6 @@ function AudioViewer({ src }) {
101
149
}
102
150
} , [ ] ) ;
103
151
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
- }
144
152
if ( waveSurf . current ) {
145
153
waveSurf . current . on ( 'finish' , ( ) => {
146
154
setPlay ( play ) ;
0 commit comments