11import React , { useContext , useEffect } from 'react' ;
22import { CometChatContext , CometChatContextType , ImageType , localize } from "../.." ;
3- import { View , TouchableOpacity , Text , Image , NativeModules , FlatList , PermissionsAndroid , Alert , Platform , Linking , TextStyle , ViewProps } from "react-native" ;
3+ import { View , TouchableOpacity , Text , Image , NativeModules , FlatList , PermissionsAndroid , Alert , Platform , Linking , TextStyle , ViewProps , AppState } from "react-native" ;
44import { Style } from "./style" ;
55import { ICONS } from '../../framework/resources' ;
66import { MediaRecorderStyle , MediaRecorderStyleInterface } from './MediaRecorderStyle' ;
77
88let recordedTime = 0 , stopRecordingIntervalId : any = null ;
9+ let recordingStatedAt = 0 ;
910export interface CometChatMediaRecorderInterface {
1011 onClose ?: Function ;
1112 onPlay ?: Function ;
@@ -70,6 +71,18 @@ export const CometChatMediaRecorder = (props: CometChatMediaRecorderInterface) =
7071 }
7172 } , [ ] )
7273
74+ useEffect ( ( ) => {
75+ const subscription = AppState . addEventListener ( 'change' , ( nextAppState ) => {
76+ if ( nextAppState === 'background' ) {
77+ _onStop ( ) ;
78+ }
79+ } ) ;
80+
81+ return ( ) => {
82+ subscription . remove ( ) ;
83+ } ;
84+ } , [ ] ) ;
85+
7386 function permissionAlert ( ) {
7487 Alert . alert ( '' , localize ( "MICROPHONE_PERMISSION" ) , [
7588 {
@@ -114,6 +127,7 @@ export const CometChatMediaRecorder = (props: CometChatMediaRecorderInterface) =
114127 }
115128
116129 const startInterval = ( ) => {
130+ recordingStatedAt = Date . now ( ) ;
117131 timerIntervalId = setInterval ( timer , 1000 ) ;
118132 }
119133
@@ -131,7 +145,7 @@ export const CometChatMediaRecorder = (props: CometChatMediaRecorderInterface) =
131145 const _onStop = ( ) => {
132146 NativeModules . FileManager . releaseMediaResources ( ( result : string ) => {
133147 console . log ( time , "Filepath _stopRecorderAudio" , result ) ;
134- recordedTime = time ;
148+ recordedTime = Date . now ( ) - recordingStatedAt ;
135149 setRecordedFile ( JSON . parse ( result ) ?. file ) ;
136150 onStop && onStop ( JSON . parse ( result ) ?. file ) ;
137151 } )
@@ -164,7 +178,7 @@ export const CometChatMediaRecorder = (props: CometChatMediaRecorderInterface) =
164178 onPause && onPause ( ) ;
165179 setRecordedPlaying ( false ) ;
166180 clearTimeout ( stopRecordingIntervalId ) ;
167- } , recordedTime * 1000 )
181+ } , recordedTime )
168182 } )
169183 }
170184
@@ -246,7 +260,7 @@ export const CometChatMediaRecorder = (props: CometChatMediaRecorderInterface) =
246260 < View style = { [ Style . timerContainer , { flexDirection : Boolean ( recordedFile ) ? "row-reverse" : "row" } ] } >
247261 < Text style = { [
248262 timerTextFont && { fontFamily : timerTextFont } ,
249- timerTextColor && { color : timerTextColor } ,
263+ timerTextColor ? { color : timerTextColor } : { color : "#000" } ,
250264 timerTextstyle && { fontStyle : timerTextstyle } ,
251265 ! Boolean ( recordedFile ) && { marginRight : 10 }
252266 ] as TextStyle } > { formatTime ( time ) } </ Text >
0 commit comments