Skip to content

Commit 0bba6bd

Browse files
authored
Merge pull request #4 from zhangmengjia/sig
fix:修复录制时录制状态异常问题,播放时状态异常等问题
2 parents bbffebe + 9aadd4f commit 0bba6bd

File tree

11 files changed

+181
-175
lines changed

11 files changed

+181
-175
lines changed
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
/**
2+
* Use these variables when you tailor your ArkTS code. They must be of the const type.
3+
*/
4+
export const HAR_VERSION = '2.0.3-0.0.2';
5+
export const BUILD_MODE_NAME = 'debug';
6+
export const DEBUG = true;
7+
export const TARGET_NAME = 'default';
8+
9+
/**
10+
* BuildProfile Class is used only for compatibility purposes.
11+
*/
112
export default class BuildProfile {
2-
static readonly HAR_VERSION = '2.0.3-0.0.1';
3-
static readonly BUILD_MODE_NAME = 'debug';
4-
static readonly DEBUG = true;
5-
static readonly TARGET_NAME = 'default';
13+
static readonly HAR_VERSION = HAR_VERSION;
14+
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
15+
static readonly DEBUG = DEBUG;
16+
static readonly TARGET_NAME = TARGET_NAME;
617
}

harmony/audio_toolkit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
> 模板版本:v0.0.1
1+
> 模板版本:v0.0.2
22
33
<p align="center">
44
<h1 align="center"> <code>@react-native-clipboard/clipboard</code> </h1>

harmony/audio_toolkit/oh-package.json5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"types": "",
44
"devDependencies": {},
55
"name": "@react-native-community/audio-toolkit",
6-
"version": "2.0.3-0.0.1",
6+
"version": "2.0.3-0.0.2",
77
"description": "",
88
"main": "index.ets",
99
"dependencies": {
10-
"@rnoh/react-native-openharmony": "file:../react_native_openharmony"
10+
"@rnoh/react-native-openharmony": "file:../libs/react_native_openharmony-5.0.0.490.har"
1111
}
1212
}

harmony/audio_toolkit/src/main/ets/RNCAudioPlayerTurboModule.ts

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
2-
import { TM } from "@rnoh/react-native-openharmony/generated/ts"
32
import { BusinessError } from '@ohos.base';
43
import media from '@ohos.multimedia.media';
5-
import common from '@ohos.app.ability.common';
64
import fs from '@ohos.file.fs';
75
import logger from './Logger';
8-
const TAG = "RCTAudioPlayerTurboModule"
9-
interface PlayConfig{
6+
7+
interface PlayConfig {
108
volume?: number,
119
pan?: number,
1210
wakeLock?: boolean,
@@ -17,55 +15,86 @@ interface PlayConfig{
1715
continueToPlayInBackground?: boolean
1816
}
1917

20-
interface PlayInfo{
18+
interface PlayInfo {
2119
duration: number,
2220
position: number
2321
}
2422

23+
interface Error {
24+
err: string,
25+
message?: string
26+
}
27+
28+
enum ConfigKey {
29+
LOOPING = 'looping',
30+
SPEED = 'speed',
31+
VOLUME = 'volume',
32+
INITIALIZED = 'initialized'
33+
}
34+
35+
enum StateChange {
36+
IDLE = 'idle',
37+
INITIALIZED = 'initialized',
38+
PREPARED = 'prepared',
39+
PLAYING = 'playing',
40+
PAUSED = 'paused',
41+
COMPLETED = 'completed',
42+
STOPPED = 'stopped',
43+
RELEASED = 'released',
44+
}
45+
2546
export class RCTAudioPlayerTurboModule extends TurboModule {
26-
private isSeek: boolean = true;
47+
private readonly SANDBOX_START = '/data/storage';
48+
private readonly FILE_MANAGER_START = 'file://docs';
49+
private readonly FD_PATH = 'fd://';
2750
private playerMap: Map<number, media.AVPlayer> = new Map()
2851
private playConfigMap: Map<number, PlayConfig> = new Map()
2952
private playInfoMap: Map<number, PlayInfo> = new Map()
30-
private playSeekCallbacks: Map<number, (err: string, result?: PlayInfo) => void> = new Map()
53+
private playSeekCallbacks: Map<number, (err: string | Error, result?: PlayInfo) => void> = new Map()
54+
3155
constructor(protected ctx: TurboModuleContext) {
3256
super(ctx);
3357
this.ctx = ctx
3458
this.onBackground()
3559
}
36-
setPlayer(playerId: number, player: media.AVPlayer){
60+
61+
setPlayer(playerId: number, player: media.AVPlayer) {
3762
this.playerMap.set(playerId, player)
3863
}
39-
getPlayer(playerId: number) : media.AVPlayer{
64+
65+
getPlayer(playerId: number): media.AVPlayer {
4066
const player = this.playerMap.get(playerId)
4167
return player
4268
}
69+
4370
applyConfig(playerId: number) {
4471
const player = this.playerMap.get(playerId)
4572
const config = this.playConfigMap.get(playerId)
4673
if (!config || !player) {
4774
return
4875
}
49-
if (player.state === 'initialized') {
76+
if (player.state === ConfigKey.INITIALIZED) {
5077
return
5178
}
5279
Object.keys(config).forEach(key => {
53-
if (key === 'looping') {
54-
player.loop = config['looping']
55-
} else if (key === 'speed') {
56-
player.setSpeed(config['speed'])
57-
} else if (key === 'volume') {
58-
player.setVolume(config['volume'])
80+
if (key === ConfigKey.LOOPING) {
81+
player.loop = config[ConfigKey.LOOPING]
82+
} else if (key === ConfigKey.SPEED) {
83+
player.setSpeed(config[ConfigKey.SPEED])
84+
} else if (key === ConfigKey.VOLUME) {
85+
player.setVolume(config[ConfigKey.VOLUME])
5986
}
6087
})
6188
}
89+
6290
setConfig(playerId: number, config: PlayConfig) {
6391
const oldConfig = this.playConfigMap.get(playerId) || {}
6492
Object.keys(config).forEach(key => {
6593
oldConfig[key] = config[key]
6694
})
6795
this.playConfigMap.set(playerId, oldConfig)
6896
}
97+
6998
set(playerId: number, config: PlayConfig, next: (err: string, result: object) => void) {
7099
const player = this.playerMap.get(playerId)
71100
if (!player) {
@@ -77,12 +106,14 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
77106
this.applyConfig(playerId)
78107
next('', {})
79108
}
109+
80110
onBackground() {
81111
this.ctx.rnInstance.subscribeToLifecycleEvents('BACKGROUND', () => {
82112
logger.debug(`app state is BACKGROUND`)
83113
this.pauseOnBackground()
84114
})
85115
}
116+
86117
pauseOnBackground() {
87118
this.playConfigMap.forEach((config, playerId) => {
88119
if (!config.continueToPlayInBackground) {
@@ -93,15 +124,18 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
93124
}
94125
})
95126
}
127+
96128
emit(name: string, data: object) {
97129
this.ctx.rnInstance.emitDeviceEvent(name, data)
98130
}
131+
99132
toEmit(playerId: number, name: string, data: object) {
100133
this.emit(`RCTAudioPlayerEvent:${playerId}`, {
101134
event: name,
102135
data
103136
})
104137
}
138+
105139
setAVPlayerCallback(avPlayer: media.AVPlayer, playerId: number) {
106140
// seek操作结果回调函数
107141
avPlayer.on('seekDone', (seekDoneTime: number) => {
@@ -120,27 +154,24 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
120154
avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
121155
logger.debug(`stateChange:${state}`)
122156
switch (state) {
123-
case 'idle': // 成功调用reset接口后触发该状态机上报
157+
case StateChange.IDLE: // 成功调用reset接口后触发该状态机上报
124158
avPlayer.release(); // 调用release接口销毁实例对象
125159
break;
126-
case 'initialized': // avplayer 设置播放源后触发该状态上报
160+
case StateChange.INITIALIZED: // avplayer 设置播放源后触发该状态上报
127161
avPlayer.prepare();
128162
break;
129-
case 'prepared': // prepare调用成功后上报该状态机
163+
case StateChange.PREPARED: // prepare调用成功后上报该状态机
130164
logger.debug(`prepared called.to and apply config and play`)
131165
this.applyConfig(playerId)
132-
avPlayer.play(); // 调用播放接口开始播放
133166
break;
134-
case 'playing': // play成功调用后触发该状态机上报
167+
case StateChange.PLAYING: // play成功调用后触发该状态机上报
135168
break;
136-
case 'paused': // pause成功调用后触发该状态机上报
169+
case StateChange.PAUSED: // pause成功调用后触发该状态机上报
137170
logger.debug('paused called.');
138-
this.toEmit(playerId, 'pause', {
139-
message: 'player paused'
140-
})
141171
break;
142-
case 'completed': // 播放结束后触发该状态机上报
172+
case StateChange.COMPLETED: // 播放结束后触发该状态机上报
143173
logger.debug('completed called.');
174+
avPlayer.seek(0)
144175
this.toEmit(playerId, 'ended', {
145176
message: 'play completed'
146177
})
@@ -149,11 +180,11 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
149180
this.destroy(playerId)
150181
}
151182
break;
152-
case 'stopped': // stop接口成功调用后触发该状态机上报
183+
case StateChange.STOPPED: // stop接口成功调用后触发该状态机上报
153184
logger.debug('Player state stopped called.');
154185
avPlayer.reset(); // 调用reset接口初始化avplayer状态
155186
break;
156-
case 'released':
187+
case StateChange.RELEASED:
157188
break;
158189
default:
159190
break;
@@ -163,8 +194,8 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
163194
const call = this.playSeekCallbacks.get(playerId)
164195
if (call) {
165196
call(``, this.getInfo(playerId))
197+
this.playSeekCallbacks.delete(playerId)
166198
}
167-
this.playSeekCallbacks.delete(playerId)
168199
this.toEmit(playerId, 'seeked', {
169200
message: 'seek completed'
170201
})
@@ -205,49 +236,55 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
205236
}
206237
})
207238
}
239+
208240
async createPlayer(pathStr: string, playerId: number) {
209241
logger.debug(`createPlayer path:${pathStr}`)
210-
try{
242+
try {
211243
const avPlayer: media.AVPlayer = await media.createAVPlayer()
212244
this.setAVPlayerCallback(avPlayer, playerId)
213245
if (pathStr.startsWith('http')) {
214246
avPlayer.url = pathStr
215247
} else {
216-
let fdPath = 'fd: //'
248+
let fdPath = this.FD_PATH
217249
const context = this.ctx.uiAbilityContext
218250
let path = context.filesDir + '/' + pathStr
219-
if (pathStr.startsWith('/data/storage')) {
251+
if (pathStr.startsWith(this.SANDBOX_START) || pathStr.startsWith(this.FILE_MANAGER_START)) {
220252
path = pathStr
221253
}
222-
logger.debug(`file path:${path}}`)
254+
logger.debug(`file path:${path}`)
223255
const file = await fs.open(path)
224256
fdPath = fdPath + file.fd
257+
logger.debug(`fdPath:${fdPath}`)
225258
avPlayer.url = fdPath
226259
}
227260
this.setPlayer(playerId, avPlayer)
228261
} catch (e) {
229262
logger.warn(`createPlayer err:${JSON.stringify(e)}`)
230263
}
231264
}
265+
232266
getInfo(playerId: number) {
233267
const info = this.playInfoMap.get(playerId)
234268
return info
235269
}
236-
getCurrentTime(playerId: number, callback:(err: string, result?: PlayInfo) => void) {
270+
271+
getCurrentTime(playerId: number, callback: (err: string, result?: PlayInfo) => void) {
237272
const info = this.playInfoMap.get(playerId)
238273
if (info) {
239274
callback('', info)
240275
} else {
241276
callback('not found player')
242277
}
243278
}
279+
244280
prepare(playerId: number, path: string, option: PlayConfig, next: () => void) {
245281
logger.debug(`prepare start`)
246282
this.setConfig(playerId, option)
247283
this.createPlayer(path, playerId).then(() => {
248284
next()
249285
})
250286
}
287+
251288
checkPlayer(playerId: number, next: (err?: string) => void) {
252289
const hasPlayer = this.playerMap.has(playerId)
253290
if (hasPlayer) {
@@ -267,7 +304,7 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
267304
const player = this.getPlayer(playerId)
268305
await player.play()
269306
callback('', this.getInfo(playerId))
270-
} catch(e) {
307+
} catch (e) {
271308
callback?.(`player call play function err:${JSON.stringify(e)}`)
272309
}
273310
}
@@ -279,6 +316,9 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
279316
}
280317
const player = this.getPlayer(playerId)
281318
await player.pause()
319+
this.toEmit(playerId, 'pause', {
320+
message: 'player paused'
321+
})
282322
callback('', this.getInfo(playerId))
283323
}
284324

@@ -296,14 +336,19 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
296336
} else {
297337
const oldCall = this.playSeekCallbacks.get(playerId)
298338
if (oldCall) {
299-
oldCall(`seek fail.stopped before seek operation counld finish`)
339+
let err: Error = {
340+
err: 'seekfail',
341+
message: 'stopped before seek operation counld finish'
342+
}
343+
oldCall(err)
300344
this.playSeekCallbacks.delete(playerId)
301345
}
302346
this.playSeekCallbacks.set(playerId, callback)
303347
player.seek(0)
304348
await player.pause()
305349
}
306350
}
351+
307352
destroy(playerId: number, callback?: () => void) {
308353
logger.debug(`destroy start`)
309354
const player = this.getPlayer(playerId)
@@ -316,18 +361,23 @@ export class RCTAudioPlayerTurboModule extends TurboModule {
316361
callback()
317362
}
318363
}
364+
319365
resume(playerId: number, callback: () => void) {
320366
this.play(playerId, callback)
321367
}
368+
322369
async seek(playerId: number, position: number, callback: () => void) {
323-
logger.debug(`seek:${position}`)
370+
logger.info(`seekbar:${position}`)
324371
if (!this.checkPlayer(playerId, callback)) {
325372
return
326373
}
327374
const player = this.getPlayer(playerId)
328375
const oldCall = this.playSeekCallbacks.get(playerId)
329376
if (oldCall) {
330-
oldCall(`seek fail`)
377+
let err: Error = {
378+
err: 'seekfail'
379+
}
380+
oldCall(err)
331381
this.playSeekCallbacks.delete(playerId)
332382
}
333383
this.playSeekCallbacks.set(playerId, callback)

0 commit comments

Comments
 (0)