utils.uts
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { getAbilityContext } from '@dcloudio/uni-runtime';
import common from '@ohos.app.ability.common';
import { fileIo } from '@kit.CoreFileKit';
import { util } from '@kit.ArkTS';
import { RecorderManagerOnStopResult, RecorderManagerErrorCode, IRecorderManagerFail } from '../interface.uts';
export type RecorderState = 'pause' | 'resume' | 'start' | 'stop' | 'error' | 'frameRecorded' | 'interruptionBegin' | 'interruptionEnd'
type ErrorCb = (result: IRecorderManagerFail) => void
interface Callbacks {
pause?: Function | null
resume?: Function | null
start?: Function | null
stop?: Function | null
error?: ErrorCb | null
frameRecorded?: Function | null
interruptionBegin?: Function | null
interruptionEnd?: Function | null
}
const callbacks: Callbacks = {
pause: null,
resume: null,
start: null,
stop: null,
error: null,
frameRecorded: null,
interruptionBegin: null,
interruptionEnd: null
}
export function setRecordStateCallback(state: RecorderState, cb: Function | ErrorCb | null = null) {
switch (state) {
case 'pause':
callbacks.pause = cb
break
case 'resume':
callbacks.resume = cb
break
case 'start':
callbacks.start = cb
break
case 'stop':
callbacks.stop = cb
break
case 'error':
callbacks.error = cb as ErrorCb
break
case 'frameRecorded':
callbacks.frameRecorded = cb
break
case 'interruptionBegin':
callbacks.interruptionBegin = cb
break
case 'interruptionEnd':
callbacks.interruptionEnd = cb
break
}
}
export function deleteRecordStateCallback(state: RecorderState) {
setRecordStateCallback(state)
}
export interface StateChangeRes extends RecorderManagerOnStopResult {
errMsg?: string
frameBuffer?: ArrayBuffer
isLastFrame?: boolean
doNotAskAgain?: boolean
}
export function onRecorderStateChange(state: RecorderState, res?: StateChangeRes | IRecorderManagerFail): void {
switch (state) {
case 'pause':
return callbacks.pause?.(res)
case 'resume':
return callbacks.resume?.(res)
case 'start':
return callbacks.start?.(res)
case 'stop':
return callbacks.stop?.(res)
case 'error':
return callbacks.error?.(res as IRecorderManagerFail)
case 'frameRecorded':
return callbacks.frameRecorded?.(res)
case 'interruptionBegin':
return callbacks.interruptionBegin?.(res)
case 'interruptionEnd':
return callbacks.interruptionEnd?.(res)
}
}
export function createFile(supportFormats: string[], format: string, defaultExt: string) {
const context = getAbilityContext() as common.UIAbilityContext;
const filePath = `${context.filesDir}/record/`
if (!fileIo.accessSync(filePath)) {
fileIo.mkdirSync(filePath, true)
}
const random = util.generateRandomUUID(true).replace(/-/g, '').substring(0, 8);
const fileName = `${Date.now()}_${random}.${supportFormats.includes(format ?? '') ? format?.toLocaleLowerCase() : defaultExt}`
const file: fileIo.File = fileIo.openSync(`${filePath}${fileName}`, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
return file
}
export function transformErrorCode(errCode: number): RecorderManagerErrorCode {
switch (errCode) {
case 201:
return 1107601
case 401:
return 1107606
case 5400106:
return 1107605
case 5400107:
return 1107607
}
return errCode as RecorderManagerErrorCode
}