@react-native-ohos/audio-toolkit 2.0.4-rc.1 → 2.0.4-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -136
- package/LICENSE +22 -22
- package/{harmony/audio_toolkit/OAT.xml → OAT.xml} +47 -37
- package/README.md +11 -17
- package/ReactNativeAudioToolkit.podspec +18 -18
- package/harmony/audio_toolkit/build-profile.json5 +7 -7
- package/harmony/audio_toolkit/hvigorfile.ts +1 -1
- package/harmony/audio_toolkit/index.ets +26 -25
- package/harmony/audio_toolkit/oh-package.json5 +12 -12
- package/harmony/audio_toolkit/src/main/cpp/AudioToolkitPackage.h +15 -0
- package/harmony/audio_toolkit/src/main/cpp/CMakeLists.txt +10 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/BaseReactNativeAudioToolkitPackage.h +69 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioPlayer.cpp +23 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioPlayer.h +16 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioRecorder.cpp +20 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/RNOH/generated/turbo_modules/RCTAudioRecorder.h +16 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/ComponentDescriptors.h +22 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/EventEmitters.cpp +18 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/EventEmitters.h +19 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/Props.cpp +21 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/Props.h +20 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/ShadowNodes.cpp +19 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/ShadowNodes.h +25 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/States.cpp +18 -0
- package/harmony/audio_toolkit/src/main/cpp/generated/react/renderer/components/react_native_audio_toolkit/States.h +23 -0
- package/harmony/audio_toolkit/src/main/ets/{AudioToolkitPackage.ts → AudioToolkitPackage.ets} +51 -50
- package/harmony/audio_toolkit/src/main/ets/Logger.ts +63 -63
- package/harmony/audio_toolkit/src/main/ets/RNCAudioPlayerTurboModule.ts +432 -432
- package/harmony/audio_toolkit/src/main/ets/RNCAudioRecorderTurboModule.ts +260 -260
- package/harmony/audio_toolkit/src/main/ets/generated/components/ts.ts +5 -0
- package/harmony/audio_toolkit/src/main/ets/generated/index.ets +5 -0
- package/harmony/audio_toolkit/src/main/ets/generated/ts.ts +6 -0
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/RCTAudioPlayer.ts +40 -0
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/RCTAudioRecorder.ts +24 -0
- package/harmony/audio_toolkit/src/main/ets/generated/turboModules/ts.ts +6 -0
- package/harmony/audio_toolkit/src/main/module.json5 +6 -6
- package/harmony/audio_toolkit/src/main/resources/base/element/string.json +7 -7
- package/harmony/audio_toolkit/src/main/resources/en_US/element/string.json +7 -7
- package/harmony/audio_toolkit/src/main/resources/zh_CN/element/string.json +7 -7
- package/harmony/audio_toolkit/{ts.ts → ts.ets} +25 -25
- package/harmony/audio_toolkit.har +0 -0
- package/package.json +48 -46
- package/src/Player.js +329 -329
- package/src/PlayerModule.ts +51 -51
- package/src/Recorder.js +183 -183
- package/src/RecorderModule.ts +83 -83
- package/src/index.ts +5 -5
- package/harmony/audio_toolkit/BuildProfile.ets +0 -17
- package/harmony/audio_toolkit/LICENSE +0 -21
- package/harmony/audio_toolkit/NOTICE +0 -33
- package/harmony/audio_toolkit/README.OpenSource +0 -11
- package/harmony/audio_toolkit/README.md +0 -230
|
@@ -1,433 +1,433 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MIT License
|
|
3
|
-
*
|
|
4
|
-
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
|
5
|
-
*
|
|
6
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
* in the Software without restriction, including without limitation the rights
|
|
9
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
* furnished to do so, subject to the following conditions:
|
|
12
|
-
*
|
|
13
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
* copies or substantial portions of the Software.
|
|
15
|
-
*
|
|
16
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
* SOFTWARE.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { TurboModule, type TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
26
|
-
import { type BusinessError } from '@ohos.base';
|
|
27
|
-
import media from '@ohos.multimedia.media';
|
|
28
|
-
import fs from '@ohos.file.fs';
|
|
29
|
-
import logger from './Logger';
|
|
30
|
-
|
|
31
|
-
interface PlayConfig {
|
|
32
|
-
volume?: number;
|
|
33
|
-
pan?: number;
|
|
34
|
-
wakeLock?: boolean;
|
|
35
|
-
duration?: number;
|
|
36
|
-
looping?: boolean;
|
|
37
|
-
speed?: number;
|
|
38
|
-
autoDestroy?: boolean;
|
|
39
|
-
continuesToPlayInBackground?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface PlayInfo {
|
|
43
|
-
duration: number;
|
|
44
|
-
position: number;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface Error {
|
|
48
|
-
err?: string;
|
|
49
|
-
message?: string;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
enum ConfigKey {
|
|
53
|
-
LOOPING = 'looping',
|
|
54
|
-
SPEED = 'speed',
|
|
55
|
-
VOLUME = 'volume',
|
|
56
|
-
INITIALIZED = 'initialized'
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
enum StateChange {
|
|
60
|
-
IDLE = 'idle',
|
|
61
|
-
INITIALIZED = 'initialized',
|
|
62
|
-
PREPARED = 'prepared',
|
|
63
|
-
PLAYING = 'playing',
|
|
64
|
-
PAUSED = 'paused',
|
|
65
|
-
COMPLETED = 'completed',
|
|
66
|
-
STOPPED = 'stopped',
|
|
67
|
-
RELEASED = 'released',
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export class RCTAudioPlayerTurboModule extends TurboModule {
|
|
71
|
-
private readonly SANDBOX_START = '/data/storage';
|
|
72
|
-
private readonly FILE_MANAGER_START = 'file://';
|
|
73
|
-
private readonly HTTP_START = 'http';
|
|
74
|
-
private readonly FD_PATH = 'fd://';
|
|
75
|
-
private playerMap: Map<number, media.AVPlayer> = new Map();
|
|
76
|
-
private playConfigMap: Map<number, PlayConfig> = new Map();
|
|
77
|
-
private playInfoMap: Map<number, PlayInfo> = new Map();
|
|
78
|
-
private playSeekCallbacks: Map<number, (err: string | Error, result?: PlayInfo) => void> = new Map();
|
|
79
|
-
|
|
80
|
-
constructor(protected ctx: TurboModuleContext) {
|
|
81
|
-
super(ctx);
|
|
82
|
-
this.ctx = ctx;
|
|
83
|
-
this.onBackground();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
setPlayer(playerId: number, player: media.AVPlayer): void {
|
|
87
|
-
this.playerMap.set(playerId, player);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getPlayer(playerId: number): media.AVPlayer {
|
|
91
|
-
const player = this.playerMap.get(playerId);
|
|
92
|
-
return player;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
applyConfig(playerId: number): void {
|
|
96
|
-
const player = this.playerMap.get(playerId);
|
|
97
|
-
const config = this.playConfigMap.get(playerId);
|
|
98
|
-
if (!config || !player) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (player.state === ConfigKey.INITIALIZED) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
Object.keys(config).forEach(key => {
|
|
105
|
-
if (key === ConfigKey.LOOPING) {
|
|
106
|
-
player.loop = config[ConfigKey.LOOPING];
|
|
107
|
-
} else if (key === ConfigKey.SPEED) {
|
|
108
|
-
player.setSpeed(config[ConfigKey.SPEED]);
|
|
109
|
-
} else if (key === ConfigKey.VOLUME) {
|
|
110
|
-
player.setVolume(config[ConfigKey.VOLUME]);
|
|
111
|
-
} else {
|
|
112
|
-
logger.warn('no contain condition');
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
setConfig(playerId: number, config: PlayConfig): void {
|
|
118
|
-
const oldConfig = this.playConfigMap.get(playerId) ?? {};
|
|
119
|
-
Object.keys(config).forEach(key => {
|
|
120
|
-
oldConfig[key] = config[key];
|
|
121
|
-
});
|
|
122
|
-
this.playConfigMap.set(playerId, oldConfig);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
set(playerId: number, config: PlayConfig, next: (err: string, result: object) => void): void {
|
|
126
|
-
const player = this.playerMap.get(playerId);
|
|
127
|
-
if (!player) {
|
|
128
|
-
next('not found player', {});
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
this.setConfig(playerId, config);
|
|
132
|
-
logger.debug(`set config:${JSON.stringify(config)}`);
|
|
133
|
-
this.applyConfig(playerId);
|
|
134
|
-
next('', {});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
onBackground(): void {
|
|
138
|
-
this.ctx.rnInstance.subscribeToLifecycleEvents('BACKGROUND', () => {
|
|
139
|
-
logger.debug(`app state is BACKGROUND`);
|
|
140
|
-
this.pauseOnBackground();
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
pauseOnBackground(): void {
|
|
145
|
-
this.playConfigMap.forEach((config, playerId) => {
|
|
146
|
-
if (!config.continuesToPlayInBackground) {
|
|
147
|
-
const player = this.playerMap.get(playerId);
|
|
148
|
-
if (player) {
|
|
149
|
-
player.pause();
|
|
150
|
-
this.toEmit(playerId, 'pause', {
|
|
151
|
-
message: 'Playback paused due toBackground',
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
emit(name: string, data: object): void {
|
|
159
|
-
this.ctx.rnInstance.emitDeviceEvent(name, data);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
toEmit(playerId: number, name: string, data: object): void {
|
|
163
|
-
this.emit(`RCTAudioPlayerEvent:${playerId}`, {
|
|
164
|
-
event: name,
|
|
165
|
-
data,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
setAVPlayerCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
|
|
170
|
-
// seek操作结果回调函数
|
|
171
|
-
avPlayer.on('seekDone', (seekDoneTime: number) => {
|
|
172
|
-
logger.debug(`AVPlayer seek succeeded, seek time is ${seekDoneTime}`);
|
|
173
|
-
});
|
|
174
|
-
// error回调监听函数,当avPlayer在操作过程中出现错误时调用 reset接口触发重置流程
|
|
175
|
-
avPlayer.on('error', (err: BusinessError) => {
|
|
176
|
-
logger.debug(`Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`);
|
|
177
|
-
this.toEmit(playerId, 'error', {
|
|
178
|
-
err: err.message,
|
|
179
|
-
message: 'Harmony AVPlayer error',
|
|
180
|
-
});
|
|
181
|
-
avPlayer.reset(); // 调用reset重置资源,触发idle状态
|
|
182
|
-
});
|
|
183
|
-
// 状态机变化回调函数
|
|
184
|
-
this.avPlayerStateChangeCallback(avPlayer, playerId, next);
|
|
185
|
-
avPlayer.on('seekDone', () => {
|
|
186
|
-
const call = this.playSeekCallbacks.get(playerId);
|
|
187
|
-
if (call) {
|
|
188
|
-
call(``, this.getInfo(playerId));
|
|
189
|
-
this.playSeekCallbacks.delete(playerId);
|
|
190
|
-
}
|
|
191
|
-
this.toEmit(playerId, 'seeked', {
|
|
192
|
-
message: 'seek completed',
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
const initInfo: PlayInfo = {
|
|
197
|
-
duration: 0,
|
|
198
|
-
position: 0,
|
|
199
|
-
};
|
|
200
|
-
this.playInfoMap.set(playerId, initInfo);
|
|
201
|
-
avPlayer.on('durationUpdate', (duration) => {
|
|
202
|
-
logger.debug(`durationUpdate:${duration}`);
|
|
203
|
-
const info = this.playInfoMap.get(playerId);
|
|
204
|
-
if (info) {
|
|
205
|
-
info.duration = duration;
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
avPlayer.on('timeUpdate', (time) => {
|
|
210
|
-
const info = this.playInfoMap.get(playerId);
|
|
211
|
-
if (info) {
|
|
212
|
-
info.position = time;
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
avPlayer.on('audioInterrupt', (reason) => {
|
|
216
|
-
logger.debug(`audioInterrupt start:${JSON.stringify(reason)}`);
|
|
217
|
-
this.toEmit(playerId, 'forcePause', {
|
|
218
|
-
message: 'lost audio focus, playback paused',
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
this.endOfStreamCallback(avPlayer, playerId);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
endOfStreamCallback(avPlayer: media.AVPlayer, playerId: number): void {
|
|
225
|
-
avPlayer.on('endOfStream', () => {
|
|
226
|
-
const config = this.playConfigMap.get(playerId);
|
|
227
|
-
if (config?.looping) {
|
|
228
|
-
this.toEmit(playerId, 'looped', {
|
|
229
|
-
message: 'media playback looped',
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
avPlayerStateChangeCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
|
|
236
|
-
avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
|
|
237
|
-
logger.debug(`stateChange:${state}`);
|
|
238
|
-
switch (state) {
|
|
239
|
-
case StateChange.IDLE: // 成功调用reset接口后触发该状态机上报
|
|
240
|
-
avPlayer.release(); // 调用release接口销毁实例对象
|
|
241
|
-
break;
|
|
242
|
-
case StateChange.INITIALIZED: // avplayer 设置播放源后触发该状态上报
|
|
243
|
-
avPlayer.prepare();
|
|
244
|
-
break;
|
|
245
|
-
case StateChange.PREPARED: // prepare调用成功后上报该状态机
|
|
246
|
-
logger.debug(`prepared called.to and apply config and play`);
|
|
247
|
-
this.applyConfig(playerId);
|
|
248
|
-
next(null);
|
|
249
|
-
break;
|
|
250
|
-
case StateChange.PLAYING: // play成功调用后触发该状态机上报
|
|
251
|
-
break;
|
|
252
|
-
case StateChange.PAUSED: // pause成功调用后触发该状态机上报
|
|
253
|
-
logger.debug('paused called.');
|
|
254
|
-
break;
|
|
255
|
-
case StateChange.COMPLETED: { // 播放结束后触发该状态机上报
|
|
256
|
-
logger.debug('completed called.');
|
|
257
|
-
avPlayer.seek(0);
|
|
258
|
-
this.toEmit(playerId, 'ended', {
|
|
259
|
-
message: 'play completed',
|
|
260
|
-
});
|
|
261
|
-
const config = this.playConfigMap.get(playerId);
|
|
262
|
-
if (config?.autoDestroy) {
|
|
263
|
-
this.destroy(playerId);
|
|
264
|
-
}
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
case StateChange.STOPPED: // stop接口成功调用后触发该状态机上报
|
|
268
|
-
logger.debug('Player state stopped called.');
|
|
269
|
-
avPlayer.reset(); // 调用reset接口初始化avplayer状态
|
|
270
|
-
break;
|
|
271
|
-
case StateChange.RELEASED:
|
|
272
|
-
break;
|
|
273
|
-
default:
|
|
274
|
-
break;
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async createPlayer(pathStr: string, playerId: number, next: (object?) => void): Promise<void> {
|
|
280
|
-
logger.debug(`createPlayer path:${pathStr}`);
|
|
281
|
-
try {
|
|
282
|
-
const avPlayer: media.AVPlayer = await media.createAVPlayer();
|
|
283
|
-
this.setAVPlayerCallback(avPlayer, playerId, next);
|
|
284
|
-
if (pathStr.startsWith(this.HTTP_START)) {
|
|
285
|
-
avPlayer.url = pathStr;
|
|
286
|
-
} else {
|
|
287
|
-
let fdPath = this.FD_PATH;
|
|
288
|
-
const context = this.ctx.uiAbilityContext;
|
|
289
|
-
let path = `${context.filesDir}/${pathStr}`;
|
|
290
|
-
if (pathStr.startsWith(this.SANDBOX_START) || pathStr.startsWith(this.FILE_MANAGER_START)) {
|
|
291
|
-
path = pathStr;
|
|
292
|
-
}
|
|
293
|
-
logger.debug(`file path:${path}`);
|
|
294
|
-
const file = await fs.open(path);
|
|
295
|
-
fdPath = fdPath + file.fd;
|
|
296
|
-
logger.debug(`fdPath:${fdPath}`);
|
|
297
|
-
avPlayer.url = fdPath;
|
|
298
|
-
}
|
|
299
|
-
this.setPlayer(playerId, avPlayer);
|
|
300
|
-
} catch (e) {
|
|
301
|
-
logger.warn(`createPlayer err:${JSON.stringify(e)}`);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
getInfo(playerId: number): PlayInfo {
|
|
306
|
-
const info = this.playInfoMap.get(playerId);
|
|
307
|
-
return info;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
getCurrentTime(playerId: number, callback: (err: string, result?: PlayInfo) => void): void {
|
|
311
|
-
const info = this.playInfoMap.get(playerId);
|
|
312
|
-
if (info) {
|
|
313
|
-
callback('', info);
|
|
314
|
-
} else {
|
|
315
|
-
callback('not found player');
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
prepare(playerId: number, path: string, option: PlayConfig, next: (object?) => void): void {
|
|
320
|
-
logger.debug(`prepare start`);
|
|
321
|
-
this.setConfig(playerId, option);
|
|
322
|
-
this.createPlayer(path, playerId, next).then(() => {
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
checkPlayer(playerId: number, next: (object?) => void): boolean {
|
|
327
|
-
const hasPlayer = this.playerMap.has(playerId);
|
|
328
|
-
if (hasPlayer) {
|
|
329
|
-
return true;
|
|
330
|
-
} else {
|
|
331
|
-
logger.debug('not found player')
|
|
332
|
-
let err: Error = {
|
|
333
|
-
err: 'not found player',
|
|
334
|
-
message: 'not found media player',
|
|
335
|
-
};
|
|
336
|
-
next?.(err);
|
|
337
|
-
return false;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
async play(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
|
|
342
|
-
try {
|
|
343
|
-
logger.debug(`play start`);
|
|
344
|
-
if (!this.checkPlayer(playerId, callback)) {
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
const player = this.getPlayer(playerId);
|
|
348
|
-
await player.play();
|
|
349
|
-
callback(null, this.getInfo(playerId));
|
|
350
|
-
} catch (e) {
|
|
351
|
-
let err: Error = {
|
|
352
|
-
err: 'player function err',
|
|
353
|
-
message: `player call play function err:${JSON.stringify(e)}`,
|
|
354
|
-
};
|
|
355
|
-
callback?.(err, this.getInfo(playerId));
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
async pause(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
|
|
360
|
-
logger.debug(`pause start`);
|
|
361
|
-
if (!this.checkPlayer(playerId, callback)) {
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
const player = this.getPlayer(playerId);
|
|
365
|
-
await player.pause();
|
|
366
|
-
this.toEmit(playerId, 'pause', {
|
|
367
|
-
message: 'player paused',
|
|
368
|
-
});
|
|
369
|
-
callback(null, this.getInfo(playerId));
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
async stop(playerId: number, callback: () => void): Promise<void> {
|
|
373
|
-
logger.debug(`stop start`);
|
|
374
|
-
if (!this.checkPlayer(playerId, callback)) {
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
const config = this.playConfigMap.get(playerId);
|
|
378
|
-
const player = this.getPlayer(playerId);
|
|
379
|
-
if (config?.autoDestroy) {
|
|
380
|
-
await player.pause();
|
|
381
|
-
this.destroy(playerId);
|
|
382
|
-
callback();
|
|
383
|
-
} else {
|
|
384
|
-
const oldCall = this.playSeekCallbacks.get(playerId);
|
|
385
|
-
if (oldCall) {
|
|
386
|
-
let err: Error = {
|
|
387
|
-
err: 'seekfail',
|
|
388
|
-
message: 'stopped before seek operation counld finish',
|
|
389
|
-
};
|
|
390
|
-
oldCall(err);
|
|
391
|
-
this.playSeekCallbacks.delete(playerId);
|
|
392
|
-
}
|
|
393
|
-
this.playSeekCallbacks.set(playerId, callback);
|
|
394
|
-
player.seek(0);
|
|
395
|
-
await player.pause();
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
destroy(playerId: number, callback?: () => void): void {
|
|
400
|
-
logger.debug(`destroy start`);
|
|
401
|
-
const player = this.getPlayer(playerId);
|
|
402
|
-
player?.release();
|
|
403
|
-
this.playerMap.delete(playerId);
|
|
404
|
-
this.playConfigMap.delete(playerId);
|
|
405
|
-
this.playInfoMap.delete(playerId);
|
|
406
|
-
this.playSeekCallbacks.delete(playerId);
|
|
407
|
-
if (callback) {
|
|
408
|
-
callback();
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
resume(playerId: number, callback: () => void): void {
|
|
413
|
-
this.play(playerId, callback);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
async seek(playerId: number, position: number, callback: () => void): Promise<void> {
|
|
417
|
-
logger.debug(`seekbar:${position}`);
|
|
418
|
-
if (!this.checkPlayer(playerId, callback)) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
const player = this.getPlayer(playerId);
|
|
422
|
-
const oldCall = this.playSeekCallbacks.get(playerId);
|
|
423
|
-
if (oldCall) {
|
|
424
|
-
let err: Error = {
|
|
425
|
-
err: 'seekfail',
|
|
426
|
-
};
|
|
427
|
-
oldCall(err);
|
|
428
|
-
this.playSeekCallbacks.delete(playerId);
|
|
429
|
-
}
|
|
430
|
-
this.playSeekCallbacks.set(playerId, callback);
|
|
431
|
-
player.seek(position);
|
|
432
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { TurboModule, type TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
26
|
+
import { type BusinessError } from '@ohos.base';
|
|
27
|
+
import media from '@ohos.multimedia.media';
|
|
28
|
+
import fs from '@ohos.file.fs';
|
|
29
|
+
import logger from './Logger';
|
|
30
|
+
|
|
31
|
+
interface PlayConfig {
|
|
32
|
+
volume?: number;
|
|
33
|
+
pan?: number;
|
|
34
|
+
wakeLock?: boolean;
|
|
35
|
+
duration?: number;
|
|
36
|
+
looping?: boolean;
|
|
37
|
+
speed?: number;
|
|
38
|
+
autoDestroy?: boolean;
|
|
39
|
+
continuesToPlayInBackground?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface PlayInfo {
|
|
43
|
+
duration: number;
|
|
44
|
+
position: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface Error {
|
|
48
|
+
err?: string;
|
|
49
|
+
message?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
enum ConfigKey {
|
|
53
|
+
LOOPING = 'looping',
|
|
54
|
+
SPEED = 'speed',
|
|
55
|
+
VOLUME = 'volume',
|
|
56
|
+
INITIALIZED = 'initialized'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
enum StateChange {
|
|
60
|
+
IDLE = 'idle',
|
|
61
|
+
INITIALIZED = 'initialized',
|
|
62
|
+
PREPARED = 'prepared',
|
|
63
|
+
PLAYING = 'playing',
|
|
64
|
+
PAUSED = 'paused',
|
|
65
|
+
COMPLETED = 'completed',
|
|
66
|
+
STOPPED = 'stopped',
|
|
67
|
+
RELEASED = 'released',
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class RCTAudioPlayerTurboModule extends TurboModule {
|
|
71
|
+
private readonly SANDBOX_START = '/data/storage';
|
|
72
|
+
private readonly FILE_MANAGER_START = 'file://';
|
|
73
|
+
private readonly HTTP_START = 'http';
|
|
74
|
+
private readonly FD_PATH = 'fd://';
|
|
75
|
+
private playerMap: Map<number, media.AVPlayer> = new Map();
|
|
76
|
+
private playConfigMap: Map<number, PlayConfig> = new Map();
|
|
77
|
+
private playInfoMap: Map<number, PlayInfo> = new Map();
|
|
78
|
+
private playSeekCallbacks: Map<number, (err: string | Error, result?: PlayInfo) => void> = new Map();
|
|
79
|
+
|
|
80
|
+
constructor(protected ctx: TurboModuleContext) {
|
|
81
|
+
super(ctx);
|
|
82
|
+
this.ctx = ctx;
|
|
83
|
+
this.onBackground();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
setPlayer(playerId: number, player: media.AVPlayer): void {
|
|
87
|
+
this.playerMap.set(playerId, player);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getPlayer(playerId: number): media.AVPlayer {
|
|
91
|
+
const player = this.playerMap.get(playerId);
|
|
92
|
+
return player;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
applyConfig(playerId: number): void {
|
|
96
|
+
const player = this.playerMap.get(playerId);
|
|
97
|
+
const config = this.playConfigMap.get(playerId);
|
|
98
|
+
if (!config || !player) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (player.state === ConfigKey.INITIALIZED) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
Object.keys(config).forEach(key => {
|
|
105
|
+
if (key === ConfigKey.LOOPING) {
|
|
106
|
+
player.loop = config[ConfigKey.LOOPING];
|
|
107
|
+
} else if (key === ConfigKey.SPEED) {
|
|
108
|
+
player.setSpeed(config[ConfigKey.SPEED]);
|
|
109
|
+
} else if (key === ConfigKey.VOLUME) {
|
|
110
|
+
player.setVolume(config[ConfigKey.VOLUME]);
|
|
111
|
+
} else {
|
|
112
|
+
logger.warn('no contain condition');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setConfig(playerId: number, config: PlayConfig): void {
|
|
118
|
+
const oldConfig = this.playConfigMap.get(playerId) ?? {};
|
|
119
|
+
Object.keys(config).forEach(key => {
|
|
120
|
+
oldConfig[key] = config[key];
|
|
121
|
+
});
|
|
122
|
+
this.playConfigMap.set(playerId, oldConfig);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
set(playerId: number, config: PlayConfig, next: (err: string, result: object) => void): void {
|
|
126
|
+
const player = this.playerMap.get(playerId);
|
|
127
|
+
if (!player) {
|
|
128
|
+
next('not found player', {});
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.setConfig(playerId, config);
|
|
132
|
+
logger.debug(`set config:${JSON.stringify(config)}`);
|
|
133
|
+
this.applyConfig(playerId);
|
|
134
|
+
next('', {});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
onBackground(): void {
|
|
138
|
+
this.ctx.rnInstance.subscribeToLifecycleEvents('BACKGROUND', () => {
|
|
139
|
+
logger.debug(`app state is BACKGROUND`);
|
|
140
|
+
this.pauseOnBackground();
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
pauseOnBackground(): void {
|
|
145
|
+
this.playConfigMap.forEach((config, playerId) => {
|
|
146
|
+
if (!config.continuesToPlayInBackground) {
|
|
147
|
+
const player = this.playerMap.get(playerId);
|
|
148
|
+
if (player) {
|
|
149
|
+
player.pause();
|
|
150
|
+
this.toEmit(playerId, 'pause', {
|
|
151
|
+
message: 'Playback paused due toBackground',
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
emit(name: string, data: object): void {
|
|
159
|
+
this.ctx.rnInstance.emitDeviceEvent(name, data);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
toEmit(playerId: number, name: string, data: object): void {
|
|
163
|
+
this.emit(`RCTAudioPlayerEvent:${playerId}`, {
|
|
164
|
+
event: name,
|
|
165
|
+
data,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
setAVPlayerCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
|
|
170
|
+
// seek操作结果回调函数
|
|
171
|
+
avPlayer.on('seekDone', (seekDoneTime: number) => {
|
|
172
|
+
logger.debug(`AVPlayer seek succeeded, seek time is ${seekDoneTime}`);
|
|
173
|
+
});
|
|
174
|
+
// error回调监听函数,当avPlayer在操作过程中出现错误时调用 reset接口触发重置流程
|
|
175
|
+
avPlayer.on('error', (err: BusinessError) => {
|
|
176
|
+
logger.debug(`Invoke avPlayer failed, code is ${err.code}, message is ${err.message}`);
|
|
177
|
+
this.toEmit(playerId, 'error', {
|
|
178
|
+
err: err.message,
|
|
179
|
+
message: 'Harmony AVPlayer error',
|
|
180
|
+
});
|
|
181
|
+
avPlayer.reset(); // 调用reset重置资源,触发idle状态
|
|
182
|
+
});
|
|
183
|
+
// 状态机变化回调函数
|
|
184
|
+
this.avPlayerStateChangeCallback(avPlayer, playerId, next);
|
|
185
|
+
avPlayer.on('seekDone', () => {
|
|
186
|
+
const call = this.playSeekCallbacks.get(playerId);
|
|
187
|
+
if (call) {
|
|
188
|
+
call(``, this.getInfo(playerId));
|
|
189
|
+
this.playSeekCallbacks.delete(playerId);
|
|
190
|
+
}
|
|
191
|
+
this.toEmit(playerId, 'seeked', {
|
|
192
|
+
message: 'seek completed',
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const initInfo: PlayInfo = {
|
|
197
|
+
duration: 0,
|
|
198
|
+
position: 0,
|
|
199
|
+
};
|
|
200
|
+
this.playInfoMap.set(playerId, initInfo);
|
|
201
|
+
avPlayer.on('durationUpdate', (duration) => {
|
|
202
|
+
logger.debug(`durationUpdate:${duration}`);
|
|
203
|
+
const info = this.playInfoMap.get(playerId);
|
|
204
|
+
if (info) {
|
|
205
|
+
info.duration = duration;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
avPlayer.on('timeUpdate', (time) => {
|
|
210
|
+
const info = this.playInfoMap.get(playerId);
|
|
211
|
+
if (info) {
|
|
212
|
+
info.position = time;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
avPlayer.on('audioInterrupt', (reason) => {
|
|
216
|
+
logger.debug(`audioInterrupt start:${JSON.stringify(reason)}`);
|
|
217
|
+
this.toEmit(playerId, 'forcePause', {
|
|
218
|
+
message: 'lost audio focus, playback paused',
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
this.endOfStreamCallback(avPlayer, playerId);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
endOfStreamCallback(avPlayer: media.AVPlayer, playerId: number): void {
|
|
225
|
+
avPlayer.on('endOfStream', () => {
|
|
226
|
+
const config = this.playConfigMap.get(playerId);
|
|
227
|
+
if (config?.looping) {
|
|
228
|
+
this.toEmit(playerId, 'looped', {
|
|
229
|
+
message: 'media playback looped',
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
avPlayerStateChangeCallback(avPlayer: media.AVPlayer, playerId: number, next: (object?) => void): void {
|
|
236
|
+
avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
|
|
237
|
+
logger.debug(`stateChange:${state}`);
|
|
238
|
+
switch (state) {
|
|
239
|
+
case StateChange.IDLE: // 成功调用reset接口后触发该状态机上报
|
|
240
|
+
avPlayer.release(); // 调用release接口销毁实例对象
|
|
241
|
+
break;
|
|
242
|
+
case StateChange.INITIALIZED: // avplayer 设置播放源后触发该状态上报
|
|
243
|
+
avPlayer.prepare();
|
|
244
|
+
break;
|
|
245
|
+
case StateChange.PREPARED: // prepare调用成功后上报该状态机
|
|
246
|
+
logger.debug(`prepared called.to and apply config and play`);
|
|
247
|
+
this.applyConfig(playerId);
|
|
248
|
+
next(null);
|
|
249
|
+
break;
|
|
250
|
+
case StateChange.PLAYING: // play成功调用后触发该状态机上报
|
|
251
|
+
break;
|
|
252
|
+
case StateChange.PAUSED: // pause成功调用后触发该状态机上报
|
|
253
|
+
logger.debug('paused called.');
|
|
254
|
+
break;
|
|
255
|
+
case StateChange.COMPLETED: { // 播放结束后触发该状态机上报
|
|
256
|
+
logger.debug('completed called.');
|
|
257
|
+
avPlayer.seek(0);
|
|
258
|
+
this.toEmit(playerId, 'ended', {
|
|
259
|
+
message: 'play completed',
|
|
260
|
+
});
|
|
261
|
+
const config = this.playConfigMap.get(playerId);
|
|
262
|
+
if (config?.autoDestroy) {
|
|
263
|
+
this.destroy(playerId);
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case StateChange.STOPPED: // stop接口成功调用后触发该状态机上报
|
|
268
|
+
logger.debug('Player state stopped called.');
|
|
269
|
+
avPlayer.reset(); // 调用reset接口初始化avplayer状态
|
|
270
|
+
break;
|
|
271
|
+
case StateChange.RELEASED:
|
|
272
|
+
break;
|
|
273
|
+
default:
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async createPlayer(pathStr: string, playerId: number, next: (object?) => void): Promise<void> {
|
|
280
|
+
logger.debug(`createPlayer path:${pathStr}`);
|
|
281
|
+
try {
|
|
282
|
+
const avPlayer: media.AVPlayer = await media.createAVPlayer();
|
|
283
|
+
this.setAVPlayerCallback(avPlayer, playerId, next);
|
|
284
|
+
if (pathStr.startsWith(this.HTTP_START)) {
|
|
285
|
+
avPlayer.url = pathStr;
|
|
286
|
+
} else {
|
|
287
|
+
let fdPath = this.FD_PATH;
|
|
288
|
+
const context = this.ctx.uiAbilityContext;
|
|
289
|
+
let path = `${context.filesDir}/${pathStr}`;
|
|
290
|
+
if (pathStr.startsWith(this.SANDBOX_START) || pathStr.startsWith(this.FILE_MANAGER_START)) {
|
|
291
|
+
path = pathStr;
|
|
292
|
+
}
|
|
293
|
+
logger.debug(`file path:${path}`);
|
|
294
|
+
const file = await fs.open(path);
|
|
295
|
+
fdPath = fdPath + file.fd;
|
|
296
|
+
logger.debug(`fdPath:${fdPath}`);
|
|
297
|
+
avPlayer.url = fdPath;
|
|
298
|
+
}
|
|
299
|
+
this.setPlayer(playerId, avPlayer);
|
|
300
|
+
} catch (e) {
|
|
301
|
+
logger.warn(`createPlayer err:${JSON.stringify(e)}`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
getInfo(playerId: number): PlayInfo {
|
|
306
|
+
const info = this.playInfoMap.get(playerId);
|
|
307
|
+
return info;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
getCurrentTime(playerId: number, callback: (err: string, result?: PlayInfo) => void): void {
|
|
311
|
+
const info = this.playInfoMap.get(playerId);
|
|
312
|
+
if (info) {
|
|
313
|
+
callback('', info);
|
|
314
|
+
} else {
|
|
315
|
+
callback('not found player');
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
prepare(playerId: number, path: string, option: PlayConfig, next: (object?) => void): void {
|
|
320
|
+
logger.debug(`prepare start`);
|
|
321
|
+
this.setConfig(playerId, option);
|
|
322
|
+
this.createPlayer(path, playerId, next).then(() => {
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
checkPlayer(playerId: number, next: (object?) => void): boolean {
|
|
327
|
+
const hasPlayer = this.playerMap.has(playerId);
|
|
328
|
+
if (hasPlayer) {
|
|
329
|
+
return true;
|
|
330
|
+
} else {
|
|
331
|
+
logger.debug('not found player')
|
|
332
|
+
let err: Error = {
|
|
333
|
+
err: 'not found player',
|
|
334
|
+
message: 'not found media player',
|
|
335
|
+
};
|
|
336
|
+
next?.(err);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async play(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
|
|
342
|
+
try {
|
|
343
|
+
logger.debug(`play start`);
|
|
344
|
+
if (!this.checkPlayer(playerId, callback)) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const player = this.getPlayer(playerId);
|
|
348
|
+
await player.play();
|
|
349
|
+
callback(null, this.getInfo(playerId));
|
|
350
|
+
} catch (e) {
|
|
351
|
+
let err: Error = {
|
|
352
|
+
err: 'player function err',
|
|
353
|
+
message: `player call play function err:${JSON.stringify(e)}`,
|
|
354
|
+
};
|
|
355
|
+
callback?.(err, this.getInfo(playerId));
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async pause(playerId: number, callback: (object?, result?: PlayInfo) => void): Promise<void> {
|
|
360
|
+
logger.debug(`pause start`);
|
|
361
|
+
if (!this.checkPlayer(playerId, callback)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const player = this.getPlayer(playerId);
|
|
365
|
+
await player.pause();
|
|
366
|
+
this.toEmit(playerId, 'pause', {
|
|
367
|
+
message: 'player paused',
|
|
368
|
+
});
|
|
369
|
+
callback(null, this.getInfo(playerId));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async stop(playerId: number, callback: () => void): Promise<void> {
|
|
373
|
+
logger.debug(`stop start`);
|
|
374
|
+
if (!this.checkPlayer(playerId, callback)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const config = this.playConfigMap.get(playerId);
|
|
378
|
+
const player = this.getPlayer(playerId);
|
|
379
|
+
if (config?.autoDestroy) {
|
|
380
|
+
await player.pause();
|
|
381
|
+
this.destroy(playerId);
|
|
382
|
+
callback();
|
|
383
|
+
} else {
|
|
384
|
+
const oldCall = this.playSeekCallbacks.get(playerId);
|
|
385
|
+
if (oldCall) {
|
|
386
|
+
let err: Error = {
|
|
387
|
+
err: 'seekfail',
|
|
388
|
+
message: 'stopped before seek operation counld finish',
|
|
389
|
+
};
|
|
390
|
+
oldCall(err);
|
|
391
|
+
this.playSeekCallbacks.delete(playerId);
|
|
392
|
+
}
|
|
393
|
+
this.playSeekCallbacks.set(playerId, callback);
|
|
394
|
+
player.seek(0);
|
|
395
|
+
await player.pause();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
destroy(playerId: number, callback?: () => void): void {
|
|
400
|
+
logger.debug(`destroy start`);
|
|
401
|
+
const player = this.getPlayer(playerId);
|
|
402
|
+
player?.release();
|
|
403
|
+
this.playerMap.delete(playerId);
|
|
404
|
+
this.playConfigMap.delete(playerId);
|
|
405
|
+
this.playInfoMap.delete(playerId);
|
|
406
|
+
this.playSeekCallbacks.delete(playerId);
|
|
407
|
+
if (callback) {
|
|
408
|
+
callback();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
resume(playerId: number, callback: () => void): void {
|
|
413
|
+
this.play(playerId, callback);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async seek(playerId: number, position: number, callback: () => void): Promise<void> {
|
|
417
|
+
logger.debug(`seekbar:${position}`);
|
|
418
|
+
if (!this.checkPlayer(playerId, callback)) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const player = this.getPlayer(playerId);
|
|
422
|
+
const oldCall = this.playSeekCallbacks.get(playerId);
|
|
423
|
+
if (oldCall) {
|
|
424
|
+
let err: Error = {
|
|
425
|
+
err: 'seekfail',
|
|
426
|
+
};
|
|
427
|
+
oldCall(err);
|
|
428
|
+
this.playSeekCallbacks.delete(playerId);
|
|
429
|
+
}
|
|
430
|
+
this.playSeekCallbacks.set(playerId, callback);
|
|
431
|
+
player.seek(position);
|
|
432
|
+
}
|
|
433
433
|
}
|