@react-native-ohos/audio-toolkit 2.0.4-rc.1
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 +136 -0
- package/LICENSE +22 -0
- package/README.md +17 -0
- package/ReactNativeAudioToolkit.podspec +18 -0
- package/banner.png +0 -0
- package/harmony/audio_toolkit/BuildProfile.ets +17 -0
- package/harmony/audio_toolkit/LICENSE +21 -0
- package/harmony/audio_toolkit/NOTICE +33 -0
- package/harmony/audio_toolkit/OAT.xml +38 -0
- package/harmony/audio_toolkit/README.OpenSource +11 -0
- package/harmony/audio_toolkit/README.md +230 -0
- package/harmony/audio_toolkit/build-profile.json5 +8 -0
- package/harmony/audio_toolkit/hvigorfile.ts +1 -0
- package/harmony/audio_toolkit/index.ets +25 -0
- package/harmony/audio_toolkit/oh-package.json5 +12 -0
- package/harmony/audio_toolkit/src/main/ets/AudioToolkitPackage.ts +51 -0
- package/harmony/audio_toolkit/src/main/ets/Logger.ts +64 -0
- package/harmony/audio_toolkit/src/main/ets/RNCAudioPlayerTurboModule.ts +433 -0
- package/harmony/audio_toolkit/src/main/ets/RNCAudioRecorderTurboModule.ts +261 -0
- package/harmony/audio_toolkit/src/main/module.json5 +7 -0
- package/harmony/audio_toolkit/src/main/resources/base/element/string.json +8 -0
- package/harmony/audio_toolkit/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/audio_toolkit/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/audio_toolkit/ts.ts +26 -0
- package/harmony/audio_toolkit.har +0 -0
- package/icon.png +0 -0
- package/package.json +47 -0
- package/src/Player.js +329 -0
- package/src/PlayerModule.ts +51 -0
- package/src/Recorder.js +183 -0
- package/src/RecorderModule.ts +83 -0
- package/src/index.ts +5 -0
|
@@ -0,0 +1,261 @@
|
|
|
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 TM } from '@rnoh/react-native-openharmony/generated/ts';
|
|
27
|
+
import { type BusinessError } from '@ohos.base';
|
|
28
|
+
import media from '@ohos.multimedia.media';
|
|
29
|
+
import fs from '@ohos.file.fs';
|
|
30
|
+
import abilityAccessCtrl, { type Permissions } from '@ohos.abilityAccessCtrl';
|
|
31
|
+
import picker from '@ohos.file.picker';
|
|
32
|
+
import logger from './Logger';
|
|
33
|
+
|
|
34
|
+
const TAG = 'RCTAudioRecorderTurboModule';
|
|
35
|
+
|
|
36
|
+
const PERMISSIONS: Array<Permissions> = [
|
|
37
|
+
'ohos.permission.MICROPHONE',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
enum FormatType {
|
|
41
|
+
CFT_MPEG_4 = 'mp4',
|
|
42
|
+
CFT_MPEG_4A = 'm4a'
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class RCTAudioRecorderTurboModule extends TurboModule {
|
|
46
|
+
private _file: fs.File;
|
|
47
|
+
// 音频参数
|
|
48
|
+
private avRecorder: media.AVRecorder | undefined = undefined;
|
|
49
|
+
private avProfile: media.AVRecorderProfile;
|
|
50
|
+
private avConfig: media.AVRecorderConfig;
|
|
51
|
+
private readonly AUDIOBITRATE_DEFAULT = 48000;
|
|
52
|
+
private readonly AUDIOCHANNELS_DEFAULT = 2;
|
|
53
|
+
private readonly AUDIOSAMPLERATE = 48000;
|
|
54
|
+
private readonly FD_PATH = 'fd://';
|
|
55
|
+
|
|
56
|
+
constructor(protected ctx: TurboModuleContext) {
|
|
57
|
+
super(ctx);
|
|
58
|
+
this.ctx = ctx;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 注册audioRecorder回调函数
|
|
62
|
+
setAudioRecorderCallback(recorderId: number): void {
|
|
63
|
+
if (this.avRecorder !== undefined) {
|
|
64
|
+
// 状态机变化回调函数
|
|
65
|
+
this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
|
|
66
|
+
this.toEmit(recorderId, 'info', {
|
|
67
|
+
event: 'info',
|
|
68
|
+
data: {
|
|
69
|
+
info: {
|
|
70
|
+
what: state,
|
|
71
|
+
extra: reason,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
// 错误上报回调函数
|
|
77
|
+
this.avRecorder.on('error', (err: BusinessError) => {
|
|
78
|
+
this.toEmit(recorderId, 'error', {
|
|
79
|
+
event: 'err',
|
|
80
|
+
message: 'harmony MediaRecorder error',
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
emit(name: string, data: object): void {
|
|
87
|
+
this.ctx.rnInstance.emitDeviceEvent(name, data);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
toEmit(recorderId: number, name: string, data: object): void {
|
|
91
|
+
this.emit(`RCTAudioRecorderEvent:${recorderId}`, { event: name, data });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 开始录制对应的流程
|
|
95
|
+
async startRecordingProcess(path: string, next: (object?, fsPath?: string) => void,
|
|
96
|
+
preparingCall?: (object?) => void): Promise<void> {
|
|
97
|
+
try {
|
|
98
|
+
let audioSaveOptions = new picker.AudioSaveOptions();
|
|
99
|
+
audioSaveOptions.newFileNames = [path];
|
|
100
|
+
let audioPicker = new picker.AudioViewPicker();
|
|
101
|
+
|
|
102
|
+
audioPicker.save(audioSaveOptions, async (err: BusinessError, audioSaveResult: Array<string>) => {
|
|
103
|
+
if (err || !audioSaveResult || audioSaveResult.length === 0) {
|
|
104
|
+
next(null);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
preparingCall(null);
|
|
108
|
+
this._file = fs.openSync(audioSaveResult[0], fs.OpenMode.READ_WRITE);
|
|
109
|
+
this.avConfig.url = this.FD_PATH + this._file.fd;
|
|
110
|
+
this.requestPermission().then(async res => {
|
|
111
|
+
if (res) {
|
|
112
|
+
await this.avRecorder.prepare(this.avConfig);
|
|
113
|
+
await this.avRecorder.start();
|
|
114
|
+
next(null, audioSaveResult[0]);
|
|
115
|
+
} else {
|
|
116
|
+
next(null);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
next(null);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 以下demo为使用fs文件系统打开沙箱地址获取媒体文件地址并通过url属性进行播放示例
|
|
126
|
+
async prepare(recorderId: number, path: string, option: TM.RCTAudioRecorder.RecorderOptions
|
|
127
|
+
, next: (object?, fsPath?: string) => void, preparingCall?: (object?) => void): Promise<void> {
|
|
128
|
+
if (path === null || path === undefined) {
|
|
129
|
+
next({
|
|
130
|
+
err: 'invalidpath',
|
|
131
|
+
stackTrace: 'Exception occurred while parsing stack trace',
|
|
132
|
+
message: 'Provided path was empty',
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (this.avRecorder !== undefined) {
|
|
137
|
+
await this.avRecorder.release();
|
|
138
|
+
this.avRecorder = undefined;
|
|
139
|
+
}
|
|
140
|
+
// 1.创建录制实例
|
|
141
|
+
this.avRecorder = await media.createAVRecorder();
|
|
142
|
+
this.setAudioRecorderCallback(recorderId);
|
|
143
|
+
this.avProfile = {
|
|
144
|
+
audioBitrate: option.bitrate ?? this.AUDIOBITRATE_DEFAULT, // 音频比特率
|
|
145
|
+
audioChannels: option.channels ?? this.AUDIOCHANNELS_DEFAULT, // 音频声道数
|
|
146
|
+
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
|
|
147
|
+
audioSampleRate: option.sampleRate ?? this.AUDIOSAMPLERATE, // 音频采样率
|
|
148
|
+
fileFormat: this.getFileFormat(option.format, path), // 封装格式,当前只支持m4a、mp4
|
|
149
|
+
};
|
|
150
|
+
logger.debug(`avProfile = ${JSON.stringify(this.avProfile)}}`);
|
|
151
|
+
this.avConfig = {
|
|
152
|
+
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, // 音频输入源,这里设置为麦克风
|
|
153
|
+
profile: this.avProfile,
|
|
154
|
+
url: '', // 参考应用文件访问与管理开发示例新建并读写一个文件
|
|
155
|
+
};
|
|
156
|
+
await this.startRecordingProcess(path, next, preparingCall);
|
|
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
|
+
|
|
182
|
+
async record(recorderId: number, next: (object?) => void): Promise<void> {
|
|
183
|
+
if (this.avRecorder !== undefined) {
|
|
184
|
+
this.avRecorder.resume();
|
|
185
|
+
next();
|
|
186
|
+
} else {
|
|
187
|
+
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async pause(recorderId: number, next: () => void): Promise<void> {
|
|
192
|
+
if (this.avRecorder !== undefined) {
|
|
193
|
+
// 1. 停止录制
|
|
194
|
+
if (this.avRecorder.state === 'started') { // 仅在started状态下调用started为合理状态切换
|
|
195
|
+
await this.avRecorder.pause();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
next();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Get clipboard image as JPG in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
203
|
+
* ```javascript
|
|
204
|
+
* async _getContent() {
|
|
205
|
+
* var content = await Clipboard.getImageJPG();
|
|
206
|
+
* }
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
// 停止录制对应的流程
|
|
211
|
+
async stop(recorderId: number, next: (object?) => void): Promise<void> {
|
|
212
|
+
if (this.avRecorder !== undefined) {
|
|
213
|
+
// 1. 停止录制
|
|
214
|
+
if (this.avRecorder.state === 'started' ||
|
|
215
|
+
this.avRecorder.state === 'paused') { // 仅在started或者paused状态下调用stop为合理状态切换
|
|
216
|
+
await this.avRecorder.stop();
|
|
217
|
+
}
|
|
218
|
+
next();
|
|
219
|
+
} else {
|
|
220
|
+
next({ code: 'notfound', recorderId: `${recorderId}not found.` });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* recorder destroy
|
|
226
|
+
* @param recorderId
|
|
227
|
+
* @param next
|
|
228
|
+
* @returns void
|
|
229
|
+
*/
|
|
230
|
+
async destroy(recorderId: number, next: (object?) => void): Promise<void> {
|
|
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
|
+
});
|
|
241
|
+
}
|
|
242
|
+
logger.debug(TAG, 'RCTAudioRecorderTurboModule destroy');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
requestPermission(): Promise<boolean> {
|
|
246
|
+
return new Promise<boolean>((resolve) => {
|
|
247
|
+
abilityAccessCtrl.createAtManager()
|
|
248
|
+
.requestPermissionsFromUser(this.ctx.uiAbilityContext, PERMISSIONS).then(result => {
|
|
249
|
+
if (result.authResults[0] === 0) {
|
|
250
|
+
resolve(true);
|
|
251
|
+
} else {
|
|
252
|
+
logger.debug(`getString,text out:用户拒绝授权`);
|
|
253
|
+
resolve(false);
|
|
254
|
+
}
|
|
255
|
+
}).catch(() => {
|
|
256
|
+
logger.debug(`getString,text out:用户拒绝授权`);
|
|
257
|
+
resolve(false);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (C) 2023 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
|
+
export * from './src/main/ets/AudioToolkitPackage'
|
|
26
|
+
export * from './src/main/ets/RNCAudioPlayerTurboModule'
|
|
Binary file
|
package/icon.png
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-native-ohos/audio-toolkit",
|
|
3
|
+
"version": "2.0.4-rc.1",
|
|
4
|
+
"description": "Cross-platform audio library for React Native",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"types": "typings/index.d.ts",
|
|
7
|
+
"harmony": {
|
|
8
|
+
"alias": "@react-native-community/audio-toolkit",
|
|
9
|
+
"codegenConfig": {
|
|
10
|
+
"specPaths": [
|
|
11
|
+
"./src"
|
|
12
|
+
]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"directories": {
|
|
16
|
+
"doc": "docs"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "react-native start"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/react-native-community/react-native-audio-toolkit#readme",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/react-native-community/react-native-audio-toolkit.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/react-native-community/react-native-audio-toolkit/issues"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"async": "^2.6.3",
|
|
32
|
+
"eventemitter3": "^1.2.0",
|
|
33
|
+
"lodash": "^4.17.19",
|
|
34
|
+
"@react-native-community/audio-toolkit": "2.0.3"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"react-native",
|
|
38
|
+
"react native",
|
|
39
|
+
"audio",
|
|
40
|
+
"audio toolkit",
|
|
41
|
+
"audio-toolkit"
|
|
42
|
+
],
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"registry": "https://registry.npmjs.org/",
|
|
45
|
+
"access": "public"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/Player.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { DeviceEventEmitter, NativeAppEventEmitter, Platform } from 'react-native';
|
|
2
|
+
import RCTAudioPlayer from './PlayerModule'
|
|
3
|
+
import async from 'async';
|
|
4
|
+
import EventEmitter from 'eventemitter3';
|
|
5
|
+
import MediaStates from '@react-native-community/audio-toolkit/src/MediaStates';
|
|
6
|
+
// Only import specific items from lodash to keep build size down
|
|
7
|
+
import filter from 'lodash/filter';
|
|
8
|
+
import identity from 'lodash/identity';
|
|
9
|
+
import last from 'lodash/last';
|
|
10
|
+
import noop from 'lodash/noop';
|
|
11
|
+
|
|
12
|
+
let playerId = 0;
|
|
13
|
+
|
|
14
|
+
export const PlaybackCategories = {
|
|
15
|
+
Playback: 1,
|
|
16
|
+
Ambient: 2,
|
|
17
|
+
SoloAmbient: 3,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const defaultPlayerOptions = {
|
|
21
|
+
autoDestroy: true,
|
|
22
|
+
continuesToPlayInBackground: false,
|
|
23
|
+
category: PlaybackCategories.Playback,
|
|
24
|
+
mixWithOthers: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents a media player
|
|
29
|
+
* @constructor
|
|
30
|
+
*/
|
|
31
|
+
class Player extends EventEmitter {
|
|
32
|
+
constructor(path, options = defaultPlayerOptions) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
this._path = path;
|
|
36
|
+
|
|
37
|
+
if (options == null) {
|
|
38
|
+
this._options = defaultPlayerOptions;
|
|
39
|
+
} else {
|
|
40
|
+
// Make sure all required options have values
|
|
41
|
+
if (options.autoDestroy == null)
|
|
42
|
+
options.autoDestroy = defaultPlayerOptions.autoDestroy;
|
|
43
|
+
if (options.continuesToPlayInBackground == null)
|
|
44
|
+
options.continuesToPlayInBackground = defaultPlayerOptions.continuesToPlayInBackground;
|
|
45
|
+
if (options.category == null)
|
|
46
|
+
options.category = defaultPlayerOptions.category;
|
|
47
|
+
if (options.mixWithOthers == null)
|
|
48
|
+
options.mixWithOthers = defaultPlayerOptions.mixWithOthers;
|
|
49
|
+
|
|
50
|
+
this._options = options;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this._playerId = playerId++;
|
|
54
|
+
this._reset();
|
|
55
|
+
|
|
56
|
+
const appEventEmitter = Platform.OS === 'ios' ? NativeAppEventEmitter : DeviceEventEmitter;
|
|
57
|
+
|
|
58
|
+
appEventEmitter.addListener(`RCTAudioPlayerEvent:${this._playerId}`, (payload: Event) => {
|
|
59
|
+
this._handleEvent(payload.event, payload.data);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_reset() {
|
|
64
|
+
this._state = MediaStates.IDLE;
|
|
65
|
+
this._volume = 1.0;
|
|
66
|
+
this._pan = 0.0;
|
|
67
|
+
this._speed = 1.0;
|
|
68
|
+
this._wakeLock = false;
|
|
69
|
+
this._duration = -1;
|
|
70
|
+
this._position = -1;
|
|
71
|
+
this._lastSync = -1;
|
|
72
|
+
this._looping = false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_storeInfo(info) {
|
|
76
|
+
if (!info) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._duration = info.duration;
|
|
81
|
+
this._position = info.position;
|
|
82
|
+
this._lastSync = Date.now();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_updateState(err, state, results) {
|
|
86
|
+
this._state = err ? MediaStates.ERROR : state;
|
|
87
|
+
|
|
88
|
+
if (err || !results) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Use last truthy value from results array as new media info
|
|
93
|
+
const info = last(filter(results, identity));
|
|
94
|
+
this._storeInfo(info);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_handleEvent(event, data) {
|
|
98
|
+
switch (event) {
|
|
99
|
+
case 'progress':
|
|
100
|
+
// TODO
|
|
101
|
+
break;
|
|
102
|
+
case 'ended':
|
|
103
|
+
this._updateState(null, MediaStates.PREPARED);
|
|
104
|
+
this._position = -1;
|
|
105
|
+
break;
|
|
106
|
+
case 'info':
|
|
107
|
+
// TODO
|
|
108
|
+
break;
|
|
109
|
+
case 'error':
|
|
110
|
+
this._state = MediaStates.ERROR;
|
|
111
|
+
break;
|
|
112
|
+
case 'pause':
|
|
113
|
+
this._state = MediaStates.PAUSED;
|
|
114
|
+
this._storeInfo(data.info);
|
|
115
|
+
break;
|
|
116
|
+
case 'forcePause':
|
|
117
|
+
this.pause();
|
|
118
|
+
break;
|
|
119
|
+
case 'looped':
|
|
120
|
+
this._position = 0;
|
|
121
|
+
this._lastSync = Date.now();
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this.emit(event, data);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
prepare(callback = noop) {
|
|
129
|
+
this._updateState(null, MediaStates.PREPARING);
|
|
130
|
+
|
|
131
|
+
const tasks = [];
|
|
132
|
+
|
|
133
|
+
// Prepare player
|
|
134
|
+
tasks.push((next) => {
|
|
135
|
+
RCTAudioPlayer.prepare(this._playerId, this._path, this._options, next);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Set initial values for player options
|
|
139
|
+
tasks.push((next) => {
|
|
140
|
+
RCTAudioPlayer.set(
|
|
141
|
+
this._playerId,
|
|
142
|
+
{
|
|
143
|
+
volume: this._volume,
|
|
144
|
+
pan: this._pan,
|
|
145
|
+
wakeLock: this._wakeLock,
|
|
146
|
+
looping: this._looping,
|
|
147
|
+
speed: this._speed,
|
|
148
|
+
},
|
|
149
|
+
next,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
async.series(tasks, (err, results) => {
|
|
154
|
+
this._updateState(err, MediaStates.PREPARED, results);
|
|
155
|
+
callback(err);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
play(callback = noop) {
|
|
162
|
+
const tasks = [];
|
|
163
|
+
// Make sure player is prepared
|
|
164
|
+
if (this._state === MediaStates.IDLE) {
|
|
165
|
+
tasks.push((next) => {
|
|
166
|
+
this.prepare(next);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Start playback
|
|
171
|
+
tasks.push((next) => {
|
|
172
|
+
RCTAudioPlayer.play(this._playerId, next);
|
|
173
|
+
});
|
|
174
|
+
async.series(tasks, (err, results) => {
|
|
175
|
+
this._updateState(err, MediaStates.PLAYING, results);
|
|
176
|
+
callback(err);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
pause(callback = noop) {
|
|
183
|
+
RCTAudioPlayer.pause(this._playerId, (err, results) => {
|
|
184
|
+
// Android emits a pause event on the native side
|
|
185
|
+
if (Platform.OS === 'ios' || Platform.OS === 'harmony') {
|
|
186
|
+
this._updateState(err, MediaStates.PAUSED, [results]);
|
|
187
|
+
}
|
|
188
|
+
callback(err);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
playPause(callback = noop) {
|
|
195
|
+
if (this._state === MediaStates.PLAYING) {
|
|
196
|
+
this.pause((err) => {
|
|
197
|
+
callback(err, true);
|
|
198
|
+
});
|
|
199
|
+
} else {
|
|
200
|
+
this.play((err) => {
|
|
201
|
+
callback(err, false);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
stop(callback = noop) {
|
|
209
|
+
RCTAudioPlayer.stop(this._playerId, (err, results) => {
|
|
210
|
+
this._updateState(err, MediaStates.PREPARED);
|
|
211
|
+
this._position = -1;
|
|
212
|
+
callback(err);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
destroy(callback = noop) {
|
|
219
|
+
this._reset();
|
|
220
|
+
RCTAudioPlayer.destroy(this._playerId, callback);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
seek(position = 0, callback = noop) {
|
|
224
|
+
// Store old state, but not if it was already SEEKING
|
|
225
|
+
if (this._state != MediaStates.SEEKING) {
|
|
226
|
+
this._preSeekState = this._state;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
this._updateState(null, MediaStates.SEEKING);
|
|
230
|
+
RCTAudioPlayer.seek(this._playerId, position, (err, results) => {
|
|
231
|
+
if (err && err.err === 'seekfail') {
|
|
232
|
+
// Seek operation was cancelled; ignore
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
this._updateState(err, this._preSeekState, [results]);
|
|
236
|
+
callback(err);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
_setIfInitialized(options, callback = noop) {
|
|
241
|
+
if (this._state >= MediaStates.PREPARED) {
|
|
242
|
+
RCTAudioPlayer.set(this._playerId, options, callback);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
set volume(value) {
|
|
247
|
+
this._volume = value;
|
|
248
|
+
this._setIfInitialized({ volume: value });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
set currentTime(value) {
|
|
252
|
+
this.seek(value);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
set wakeLock(value) {
|
|
256
|
+
this._wakeLock = value;
|
|
257
|
+
this._setIfInitialized({ wakeLock: value });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
set looping(value) {
|
|
261
|
+
this._looping = value;
|
|
262
|
+
this._setIfInitialized({ looping: value });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
set speed(value) {
|
|
266
|
+
this._speed = value;
|
|
267
|
+
this._setIfInitialized({ speed: value });
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
get currentTime() {
|
|
271
|
+
// Queue up an async call to get an accurate current time
|
|
272
|
+
RCTAudioPlayer.getCurrentTime(this._playerId, (err, results) => {
|
|
273
|
+
this._storeInfo(results);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
if (this._position < 0) {
|
|
277
|
+
return -1;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (this._state === MediaStates.PLAYING) {
|
|
281
|
+
// Estimate the current time based on the latest info we received
|
|
282
|
+
let pos = this._position + (Date.now() - this._lastSync) * this._speed;
|
|
283
|
+
pos = Math.min(pos, this._duration);
|
|
284
|
+
return pos;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return this._position;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
get volume() {
|
|
291
|
+
return this._volume;
|
|
292
|
+
}
|
|
293
|
+
get looping() {
|
|
294
|
+
return this._looping;
|
|
295
|
+
}
|
|
296
|
+
get duration() {
|
|
297
|
+
return this._duration;
|
|
298
|
+
}
|
|
299
|
+
get speed() {
|
|
300
|
+
return this._speed;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
get state() {
|
|
304
|
+
return this._state;
|
|
305
|
+
}
|
|
306
|
+
get canPlay() {
|
|
307
|
+
return this._state >= MediaStates.PREPARED;
|
|
308
|
+
}
|
|
309
|
+
get canStop() {
|
|
310
|
+
return this._state >= MediaStates.PLAYING;
|
|
311
|
+
}
|
|
312
|
+
get canPrepare() {
|
|
313
|
+
return this._state == MediaStates.IDLE;
|
|
314
|
+
}
|
|
315
|
+
get isPlaying() {
|
|
316
|
+
return this._state == MediaStates.PLAYING;
|
|
317
|
+
}
|
|
318
|
+
get isStopped() {
|
|
319
|
+
return this._state <= MediaStates.PREPARED;
|
|
320
|
+
}
|
|
321
|
+
get isPaused() {
|
|
322
|
+
return this._state == MediaStates.PAUSED;
|
|
323
|
+
}
|
|
324
|
+
get isPrepared() {
|
|
325
|
+
return this._state == MediaStates.PREPARED;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export default Player;
|