@@ -37,9 +37,9 @@ const PERMISSIONS: Array<Permissions> = [
37
37
'ohos.permission.MICROPHONE' ,
38
38
] ;
39
39
40
- interface Error {
41
- err ?: string ;
42
- message ?: string ;
40
+ enum FormatType {
41
+ CFT_MPEG_4 = 'mp4' ,
42
+ CFT_MPEG_4A = 'm4a'
43
43
}
44
44
45
45
export class RCTAudioRecorderTurboModule extends TurboModule {
@@ -141,12 +141,13 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
141
141
this . avRecorder = await media . createAVRecorder ( ) ;
142
142
this . setAudioRecorderCallback ( recorderId ) ;
143
143
this . avProfile = {
144
- audioBitrate : option . bitRate ?? this . AUDIOBITRATE_DEFAULT , // 音频比特率
144
+ audioBitrate : option . bitrate ?? this . AUDIOBITRATE_DEFAULT , // 音频比特率
145
145
audioChannels : option . channels ?? this . AUDIOCHANNELS_DEFAULT , // 音频声道数
146
146
audioCodec : media . CodecMimeType . AUDIO_AAC , // 音频编码格式,当前只支持aac
147
147
audioSampleRate : option . sampleRate ?? this . AUDIOSAMPLERATE , // 音频采样率
148
- fileFormat : media . ContainerFormatType . CFT_MPEG_4A , // 封装格式,当前只支持m4a
148
+ fileFormat : this . getFileFormat ( option . format , path ) , // 封装格式,当前只支持m4a、mp4
149
149
} ;
150
+ logger . debug ( `avProfile = ${ JSON . stringify ( this . avProfile ) } }` ) ;
150
151
this . avConfig = {
151
152
audioSourceType : media . AudioSourceType . AUDIO_SOURCE_TYPE_MIC , // 音频输入源,这里设置为麦克风
152
153
profile : this . avProfile ,
@@ -155,6 +156,29 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
155
156
await this . startRecordingProcess ( path , next , preparingCall ) ;
156
157
}
157
158
159
+ private formatFromPath ( path : String ) : media . ContainerFormatType {
160
+ if ( ! path ) {
161
+ return media . ContainerFormatType . CFT_MPEG_4A ;
162
+ }
163
+ let ext = path . substring ( path . lastIndexOf ( '.' ) + 1 ) ;
164
+ if ( ! ext ) {
165
+ return media . ContainerFormatType . CFT_MPEG_4A ;
166
+ }
167
+ return this . getFileFormat ( ext ) ;
168
+ }
169
+
170
+ getFileFormat ( format : string , path ?: string ) : media . ContainerFormatType {
171
+ if ( ! format ) {
172
+ return this . formatFromPath ( path ) ;
173
+ }
174
+ switch ( format ) {
175
+ case FormatType . CFT_MPEG_4 :
176
+ return media . ContainerFormatType . CFT_MPEG_4 ;
177
+ default :
178
+ return media . ContainerFormatType . CFT_MPEG_4A
179
+ }
180
+ }
181
+
158
182
async record ( recorderId : number , next : ( object ?) => void ) : Promise < void > {
159
183
if ( this . avRecorder !== undefined ) {
160
184
this . avRecorder . resume ( ) ;
@@ -198,46 +222,38 @@ export class RCTAudioRecorderTurboModule extends TurboModule {
198
222
}
199
223
200
224
/**
201
- * (iOS Only)
202
- * Set content of base64 image type. You can use following code to set clipboard content
203
- * ```javascript
204
- * _setContent() {
205
- * Clipboard.setImage(...);
206
- * }
207
- * ```
208
- * @param the content to be stored in the clipboard.
225
+ * recorder destroy
226
+ * @param recorderId
227
+ * @param next
228
+ * @returns void
209
229
*/
210
230
async destroy ( recorderId : number , next : ( object ?) => void ) : Promise < void > {
211
- if ( this . avRecorder !== undefined ) {
212
- if ( this . avRecorder . state === 'stopped' ) {
213
- // 2.重置
214
- await this . avRecorder . reset ( ) ;
215
- // 3.释放录制实例
216
- await this . avRecorder . release ( ) ;
217
-
218
- // 4.关闭录制文件fd
219
- fs . close ( this . _file ) ;
220
- this . toEmit ( recorderId , 'info' , {
221
- message : 'Destroyed recorder' ,
222
- } ) ;
223
- }
231
+ if ( this . avRecorder ) {
232
+ //重置
233
+ await this . avRecorder . reset ( ) ;
234
+ //释放录制实例
235
+ await this . avRecorder . release ( ) ;
236
+ //关闭录制文件fd
237
+ fs . close ( this . _file ) ;
238
+ this . toEmit ( recorderId , 'info' , {
239
+ message : 'Destroyed recorder' ,
240
+ } ) ;
224
241
}
225
242
logger . debug ( TAG , 'RCTAudioRecorderTurboModule destroy' ) ;
226
243
}
227
244
228
-
229
245
requestPermission ( ) : Promise < boolean > {
230
246
return new Promise < boolean > ( ( resolve ) => {
231
247
abilityAccessCtrl . createAtManager ( )
232
248
. requestPermissionsFromUser ( this . ctx . uiAbilityContext , PERMISSIONS ) . then ( result => {
233
249
if ( result . authResults [ 0 ] === 0 ) {
234
250
resolve ( true ) ;
235
251
} else {
236
- logger . debug ( TAG , `getString,text out:用户拒绝授权` ) ;
252
+ logger . debug ( `getString,text out:用户拒绝授权` ) ;
237
253
resolve ( false ) ;
238
254
}
239
255
} ) . catch ( ( ) => {
240
- logger . debug ( TAG , `getString,text out:用户拒绝授权` ) ;
256
+ logger . debug ( `getString,text out:用户拒绝授权` ) ;
241
257
resolve ( false ) ;
242
258
} ) ;
243
259
} ) ;
0 commit comments