1- import { useRef , useState } from 'react' ;
1+ import { useCallback , useEffect , useRef , useState } from 'react' ;
22import { Button , Linking , SafeAreaView , ScrollView , View } from 'react-native' ;
33import {
44 FinishMode ,
99 RecorderState ,
1010 UpdateFrequency ,
1111 Waveform ,
12- useRecorderPermission ,
12+ useAudioPermission ,
1313} from 'react-native-audio-waveform' ;
1414import { GestureHandlerRootView } from 'react-native-gesture-handler' ;
1515import RNFetchBlob from 'rn-fetch-blob' ;
@@ -71,8 +71,8 @@ const ListItem = ({
7171const LivePlayerComponent = ( ) => {
7272 const newRef = useRef < IRecordWaveformRef > ( null ) ;
7373 const [ recorderState , setRecorderState ] = useState ( RecorderState . stopped ) ;
74- const { checkHasRecorderPermission , getAudioRecorderPermission } =
75- useRecorderPermission ( ) ;
74+ const { checkHasAudioRecorderPermission , getAudioRecorderPermission } =
75+ useAudioPermission ( ) ;
7676
7777 const startRecording = ( ) => {
7878 newRef . current
@@ -89,7 +89,7 @@ const LivePlayerComponent = () => {
8989 title = { Strings . start }
9090 disabled = { recorderState !== RecorderState . stopped }
9191 onPress = { async ( ) => {
92- let hasPermission = await checkHasRecorderPermission ( ) ;
92+ let hasPermission = await checkHasAudioRecorderPermission ( ) ;
9393
9494 if ( hasPermission === PermissionStatus . granted ) {
9595 startRecording ( ) ;
@@ -136,27 +136,57 @@ const LivePlayerComponent = () => {
136136
137137const App = ( ) => {
138138 const [ shouldScroll , setShouldScroll ] = useState ( true ) ;
139+ const [ list , setList ] = useState < string [ ] > ( [ ] ) ;
140+ const [ hasAudioReadPermission , setHasAudioReadPermission ] = useState (
141+ PermissionStatus . granted
142+ ) ;
139143 const { fs } = RNFetchBlob ;
140144 const filePath = `${ fs . dirs . MainBundleDir } ` ;
141- const list = [
142- `${ filePath } /file_example_MP3_1MG.mp3` ,
143- `${ filePath } /file_example_MP3_700KB.mp3` ,
144- ] ;
145+
146+ const { checkHasAudioReadPermission, getAudioReadPermission } =
147+ useAudioPermission ( ) ;
148+ const checkAudioReadPermission = useCallback ( async ( ) => {
149+ const hasPermission = await checkHasAudioReadPermission ( ) ;
150+ setHasAudioReadPermission ( hasPermission ) ;
151+ if ( hasPermission ) {
152+ const newList = [
153+ `${ filePath } /file_example_MP3_1MG.mp3` ,
154+ `${ filePath } /file_example_MP3_700KB.mp3` ,
155+ ] ;
156+ setList ( newList ) ;
157+ }
158+ } , [ ] ) ;
159+
160+ const getPermissionForAudioRead = async ( ) => {
161+ const hasPermission = await getAudioReadPermission ( ) ;
162+ setHasAudioReadPermission ( hasPermission ) ;
163+ } ;
164+
165+ useEffect ( ( ) => {
166+ checkAudioReadPermission ( ) ;
167+ } , [ checkHasAudioReadPermission ] ) ;
168+
145169 return (
146170 < SafeAreaView style = { styles . container } >
147171 < GestureHandlerRootView style = { styles . container } >
148172 < View style = { styles . container } >
149173 < LivePlayerComponent />
150- < ScrollView scrollEnabled = { shouldScroll } >
151- { list . map ( ( item , index ) => (
152- < ListItem
153- key = { `${ item } ${ index } ` }
154- index = { index }
155- item = { item }
156- onPanStateChange = { value => setShouldScroll ( ! value ) }
157- />
158- ) ) }
159- </ ScrollView >
174+ { hasAudioReadPermission === PermissionStatus . granted ? (
175+ < ScrollView scrollEnabled = { shouldScroll } >
176+ { list . map ( ( item , index ) => (
177+ < ListItem
178+ key = { `${ item } ${ index } ` }
179+ index = { index }
180+ item = { item }
181+ onPanStateChange = { value => setShouldScroll ( ! value ) }
182+ />
183+ ) ) }
184+ </ ScrollView >
185+ ) : (
186+ < Button
187+ title = { Strings . getAudioReadPermission }
188+ onPress = { getPermissionForAudioRead } > </ Button >
189+ ) }
160190 </ View >
161191 </ GestureHandlerRootView >
162192 </ SafeAreaView >
0 commit comments