strategies.js
3.53 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* @Author: limin
* @Date: 2025-01-15
* @Description: 录音管理策略工厂 (Strategy Pattern)
*/
import NativeRecorder from './NativeRecorder'
import NativeSave from './NativeSave'
import PollingSave from './PollingSave'
// #ifdef APP-PLUS
import { getRecorderManagerX } from '@/uni_modules/uni-recorder-x'
// #endif
// 录音模式配置
// 'x' : 使用 uni-recorder-x (默认,支持 onFrameRecorded 实时流回调)
// 'native' : 使用 NativeRecorder (Android Java 原生长录音)
// 'uni' : 使用 uni.getRecorderManager (基础)
export const RECORDER_MODE = 'x'
// --- Recorder Strategies (录音器策略) ---
/**
* uni-recorder-x 策略
* 支持 onFrameRecorded 实时音频流回调,跨平台
*/
class UniRecorderXStrategy {
constructor() {
// #ifdef APP-PLUS
this.manager = getRecorderManagerX()
// #endif
// #ifndef APP-PLUS
this.manager = uni.getRecorderManager()
// #endif
this.name = 'uni-recorder-x'
}
start(options) {
this.manager.start(options)
}
stop() {
this.manager.stop()
}
onStart(cb) {
this.manager.onStart(cb)
}
onStop(cb) {
this.manager.onStop(cb)
}
onError(cb) {
this.manager.onError(cb)
}
/**
* 实时音频帧回调 (仅 APP-PLUS 平台有效)
* @param {Function} cb - 回调函数,参数: { base64, isLastFrame }
*/
onFrameRecorded(cb) {
// #ifdef APP-PLUS
if (this.manager.onFrameRecorded) {
this.manager.onFrameRecorded(cb)
}
// #endif
}
/**
* 实时音频帧结束回调 (仅 APP-PLUS 平台有效)
* @param {Function} cb - 回调函数,参数: { base64, isLastFrame }
*/
onFrameRecordedEnd(cb) {
// #ifdef APP-PLUS
if (this.manager.onFrameRecordedEnd) {
this.manager.onFrameRecordedEnd(cb)
}
// #endif
}
offFrameRecorded() {
// #ifdef APP-PLUS
if (this.manager.offFrameRecorded) {
this.manager.offFrameRecorded()
}
// #endif
}
}
/**
* UniApp 原生录音策略
* 封装 uni.getRecorderManager()
*/
class UniRecorderStrategy {
constructor() {
this.manager = uni.getRecorderManager()
this.name = 'uni.getRecorderManager'
}
start(options) {
this.manager.start(options)
}
stop() {
this.manager.stop()
}
onStart(cb) {
this.manager.onStart(cb)
}
onStop(cb) {
this.manager.onStop(cb)
}
onError(cb) {
this.manager.onError(cb)
}
}
// NativeRecorder 已经实现了相同的接口,直接作为策略使用
// --- Strategy Factory (策略工厂) ---
class StrategyFactory {
/**
* 获取录音器策略
* @returns {Object} 录音器实例 (Strategy)
*/
static getRecorderStrategy() {
// 优先使用 uni-recorder-x (默认)
if (RECORDER_MODE === 'x') {
// #ifdef APP-PLUS
if (plus.os.name === 'Android' || plus.os.name === 'iOS') {
return new UniRecorderXStrategy()
}
// #endif
// 非 APP 平台降级为 uni.getRecorderManager
return new UniRecorderStrategy()
}
// #ifdef APP-PLUS
if (RECORDER_MODE === 'native' && plus.os.name === 'Android') {
return NativeRecorder
}
// #endif
return new UniRecorderStrategy()
}
/**
* 获取保存策略
* @param {Object} context 上下文 (通常是 TempRecord 实例需要的参数)
* @returns {Object} 保存策略实例
*/
static getSaveStrategy(...args) {
let StrategyClass = PollingSave
// #ifdef APP-PLUS
StrategyClass = NativeSave
// #endif
return new StrategyClass(...args)
}
}
export default StrategyFactory