@react-native-ohos/react-native-audio 4.2.3-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/LICENSE +21 -0
- package/NativeAudio.ts +43 -0
- package/README.md +15 -0
- package/harmony/audio/build-profile.json5 +8 -0
- package/harmony/audio/hvigorfile.ts +2 -0
- package/harmony/audio/index.ets +6 -0
- package/harmony/audio/oh-package.json5 +12 -0
- package/harmony/audio/src/main/cpp/AudioPackage.h +16 -0
- package/harmony/audio/src/main/cpp/CMakeLists.txt +9 -0
- package/harmony/audio/src/main/cpp/generated/RNOH/generated/BaseReactNativeAudioPackage.h +65 -0
- package/harmony/audio/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNAudio.cpp +23 -0
- package/harmony/audio/src/main/cpp/generated/RNOH/generated/turbo_modules/RTNAudio.h +16 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/ComponentDescriptors.h +22 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/EventEmitters.cpp +18 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/EventEmitters.h +19 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/Props.cpp +21 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/Props.h +20 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/ShadowNodes.cpp +19 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/ShadowNodes.h +25 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/States.cpp +18 -0
- package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/States.h +23 -0
- package/harmony/audio/src/main/ets/AudioModule.ts +48 -0
- package/harmony/audio/src/main/ets/AudioPackage.ts +29 -0
- package/harmony/audio/src/main/ets/AudioRecordManager.ts +352 -0
- package/harmony/audio/src/main/ets/AudioType.ts +26 -0
- package/harmony/audio/src/main/ets/StopWatch.ts +44 -0
- package/harmony/audio/src/main/ets/generated/components/ts.ts +5 -0
- package/harmony/audio/src/main/ets/generated/index.ets +5 -0
- package/harmony/audio/src/main/ets/generated/ts.ts +6 -0
- package/harmony/audio/src/main/ets/generated/turboModules/RTNAudio.ts +32 -0
- package/harmony/audio/src/main/ets/generated/turboModules/ts.ts +5 -0
- package/harmony/audio/src/main/module.json5 +19 -0
- package/harmony/audio/src/main/resources/base/element/string.json +20 -0
- package/harmony/audio/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/audio/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/audio/ts.ts +7 -0
- package/harmony/audio.har +0 -0
- package/index.js +169 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) [2016] [Joshua Sierles]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NativeAudio.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { TurboModule } from "react-native/Libraries/TurboModule/RCTExport";
|
|
8
|
+
import { TurboModuleRegistry } from "react-native";
|
|
9
|
+
import type { Int32, Double } from 'react-native/Libraries/Types/CodegenTypes';
|
|
10
|
+
|
|
11
|
+
type AudioQualityType = 'Low' | 'Medium' | 'High';
|
|
12
|
+
|
|
13
|
+
export interface RecordingOptions {
|
|
14
|
+
SampleRate: Double,
|
|
15
|
+
Channels: Int32,
|
|
16
|
+
AudioQuality?: AudioQualityType,
|
|
17
|
+
AudioEncoding: string,
|
|
18
|
+
MeteringEnabled?: boolean,
|
|
19
|
+
MeasurementMode?: boolean,
|
|
20
|
+
AudioEncodingBitRate: Double,
|
|
21
|
+
IncludeBase64: boolean,
|
|
22
|
+
OutputFormat: string,
|
|
23
|
+
AudioSource: Int32
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PathMap {
|
|
27
|
+
FilesDirectoryPath: string,
|
|
28
|
+
CacheDirectoryPath: string,
|
|
29
|
+
TempsDirectoryPath: string,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Spec extends TurboModule {
|
|
33
|
+
prepareRecordingAtPath: (path: string, options: RecordingOptions) => Promise<void>;
|
|
34
|
+
requestAuthorization: () => Promise<boolean>;
|
|
35
|
+
startRecording: () => Promise<void>;
|
|
36
|
+
pauseRecording: () => Promise<void>;
|
|
37
|
+
resumeRecording: () => Promise<void>;
|
|
38
|
+
stopRecording: () => Promise<void>;
|
|
39
|
+
getAllPath: () => PathMap;
|
|
40
|
+
checkAuthorizationStatus: () => Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default TurboModuleRegistry.getEnforcing<Spec>("RTNAudio");
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @react-native-ohos/react-native-audio
|
|
2
|
+
|
|
3
|
+
This project is based on [react-native-audio](https://github.com/jsierles/react-native-audio)
|
|
4
|
+
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
[中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-audio.md)
|
|
8
|
+
|
|
9
|
+
[English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-audio.md)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## License
|
|
13
|
+
|
|
14
|
+
This library is licensed under [The MIT License (MIT)](https://gitee.com/openharmony-sig/rntpc_react-native-audio/blob/master/LICENSE).
|
|
15
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "generated/RNOH/generated/BaseReactNativeAudioPackage.h"
|
|
10
|
+
namespace rnoh {
|
|
11
|
+
|
|
12
|
+
class AudioPackage : public BaseReactNativeAudioPackage {
|
|
13
|
+
using Super = BaseReactNativeAudioPackage;
|
|
14
|
+
using Super::Super;
|
|
15
|
+
};
|
|
16
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.13)
|
|
2
|
+
set(CMAKE_VERBOSE_MAKEFILE on)
|
|
3
|
+
|
|
4
|
+
set(rnoh_audio_generated_dir "${CMAKE_CURRENT_SOURCE_DIR}/generated")
|
|
5
|
+
file(GLOB_RECURSE rnoh_audio_generated_SRC "${rnoh_audio_generated_dir}/**/*.cpp")
|
|
6
|
+
file(GLOB rnoh_audio_SRC CONFIGURE_DEPENDS *.cpp)
|
|
7
|
+
add_library(rnoh_audio SHARED ${rnoh_audio_SRC} ${rnoh_audio_generated_SRC})
|
|
8
|
+
target_include_directories(rnoh_audio PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${rnoh_audio_generated_dir})
|
|
9
|
+
target_link_libraries(rnoh_audio PUBLIC rnoh)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include "RNOH/Package.h"
|
|
8
|
+
#include "RNOH/ArkTSTurboModule.h"
|
|
9
|
+
#include "RNOH/generated/turbo_modules/RTNAudio.h"
|
|
10
|
+
|
|
11
|
+
namespace rnoh {
|
|
12
|
+
|
|
13
|
+
class BaseReactNativeAudioPackageTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
|
|
14
|
+
public:
|
|
15
|
+
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
|
|
16
|
+
if (name == "RTNAudio") {
|
|
17
|
+
return std::make_shared<RTNAudio>(ctx, name);
|
|
18
|
+
}
|
|
19
|
+
return nullptr;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class BaseReactNativeAudioPackageEventEmitRequestHandler : public EventEmitRequestHandler {
|
|
24
|
+
public:
|
|
25
|
+
void handleEvent(Context const &ctx) override {
|
|
26
|
+
auto eventEmitter = ctx.shadowViewRegistry->getEventEmitter<facebook::react::EventEmitter>(ctx.tag);
|
|
27
|
+
if (eventEmitter == nullptr) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
std::vector<std::string> supportedEventNames = {
|
|
32
|
+
};
|
|
33
|
+
if (std::find(supportedEventNames.begin(), supportedEventNames.end(), ctx.eventName) != supportedEventNames.end()) {
|
|
34
|
+
eventEmitter->dispatchEvent(ctx.eventName, ArkJS(ctx.env).getDynamic(ctx.payload));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class BaseReactNativeAudioPackage : public Package {
|
|
41
|
+
public:
|
|
42
|
+
BaseReactNativeAudioPackage(Package::Context ctx) : Package(ctx){};
|
|
43
|
+
|
|
44
|
+
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override {
|
|
45
|
+
return std::make_unique<BaseReactNativeAudioPackageTurboModuleFactoryDelegate>();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
std::vector<facebook::react::ComponentDescriptorProvider> createComponentDescriptorProviders() override {
|
|
49
|
+
return {
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
ComponentJSIBinderByString createComponentJSIBinderByName() override {
|
|
54
|
+
return {
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
EventEmitRequestHandlers createEventEmitRequestHandlers() override {
|
|
59
|
+
return {
|
|
60
|
+
std::make_shared<BaseReactNativeAudioPackageEventEmitRequestHandler>(),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#include "RTNAudio.h"
|
|
6
|
+
|
|
7
|
+
namespace rnoh {
|
|
8
|
+
using namespace facebook;
|
|
9
|
+
|
|
10
|
+
RTNAudio::RTNAudio(const ArkTSTurboModule::Context ctx, const std::string name) : ArkTSTurboModule(ctx, name) {
|
|
11
|
+
methodMap_ = {
|
|
12
|
+
ARK_ASYNC_METHOD_METADATA(prepareRecordingAtPath, 2),
|
|
13
|
+
ARK_ASYNC_METHOD_METADATA(requestAuthorization, 0),
|
|
14
|
+
ARK_ASYNC_METHOD_METADATA(startRecording, 0),
|
|
15
|
+
ARK_ASYNC_METHOD_METADATA(pauseRecording, 0),
|
|
16
|
+
ARK_ASYNC_METHOD_METADATA(resumeRecording, 0),
|
|
17
|
+
ARK_ASYNC_METHOD_METADATA(stopRecording, 0),
|
|
18
|
+
ARK_METHOD_METADATA(getAllPath, 0),
|
|
19
|
+
ARK_ASYNC_METHOD_METADATA(checkAuthorizationStatus, 0),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include "RNOH/ArkTSTurboModule.h"
|
|
8
|
+
|
|
9
|
+
namespace rnoh {
|
|
10
|
+
|
|
11
|
+
class JSI_EXPORT RTNAudio : public ArkTSTurboModule {
|
|
12
|
+
public:
|
|
13
|
+
RTNAudio(const ArkTSTurboModule::Context ctx, const std::string name);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
} // namespace rnoh
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateComponentDescriptorH.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#pragma once
|
|
12
|
+
|
|
13
|
+
#include <react/renderer/components/react_native_audio/ShadowNodes.h>
|
|
14
|
+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
|
|
15
|
+
|
|
16
|
+
namespace facebook {
|
|
17
|
+
namespace react {
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
} // namespace react
|
|
22
|
+
} // namespace facebook
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateEventEmitterCpp.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include <react/renderer/components/react_native_audio/EventEmitters.h>
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
namespace facebook {
|
|
15
|
+
namespace react {
|
|
16
|
+
|
|
17
|
+
} // namespace react
|
|
18
|
+
} // namespace facebook
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateEventEmitterH.js
|
|
9
|
+
*/
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
#include <react/renderer/components/view/ViewEventEmitter.h>
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
namespace facebook {
|
|
16
|
+
namespace react {
|
|
17
|
+
|
|
18
|
+
} // namespace react
|
|
19
|
+
} // namespace facebook
|
package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/Props.cpp
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GeneratePropsCpp.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include <react/renderer/components/react_native_audio/Props.h>
|
|
12
|
+
#include <react/renderer/core/PropsParserContext.h>
|
|
13
|
+
#include <react/renderer/core/propsConversions.h>
|
|
14
|
+
|
|
15
|
+
namespace facebook {
|
|
16
|
+
namespace react {
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
} // namespace react
|
|
21
|
+
} // namespace facebook
|
package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/Props.h
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GeneratePropsH.js
|
|
9
|
+
*/
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
namespace facebook {
|
|
15
|
+
namespace react {
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
} // namespace react
|
|
20
|
+
} // namespace facebook
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateShadowNodeCpp.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include <react/renderer/components/react_native_audio/ShadowNodes.h>
|
|
12
|
+
|
|
13
|
+
namespace facebook {
|
|
14
|
+
namespace react {
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
} // namespace react
|
|
19
|
+
} // namespace facebook
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateShadowNodeH.js
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#pragma once
|
|
12
|
+
|
|
13
|
+
#include <react/renderer/components/react_native_audio/EventEmitters.h>
|
|
14
|
+
#include <react/renderer/components/react_native_audio/Props.h>
|
|
15
|
+
#include <react/renderer/components/react_native_audio/States.h>
|
|
16
|
+
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
|
|
17
|
+
#include <jsi/jsi.h>
|
|
18
|
+
|
|
19
|
+
namespace facebook {
|
|
20
|
+
namespace react {
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
} // namespace react
|
|
25
|
+
} // namespace facebook
|
package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/States.cpp
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
4
|
+
*
|
|
5
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
6
|
+
* once the code is regenerated.
|
|
7
|
+
*
|
|
8
|
+
* @generated by codegen project: GenerateStateCpp.js
|
|
9
|
+
*/
|
|
10
|
+
#include <react/renderer/components/react_native_audio/States.h>
|
|
11
|
+
|
|
12
|
+
namespace facebook {
|
|
13
|
+
namespace react {
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
} // namespace react
|
|
18
|
+
} // namespace facebook
|
package/harmony/audio/src/main/cpp/generated/react/renderer/components/react_native_audio/States.h
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
5
|
+
* once the code is regenerated.
|
|
6
|
+
*
|
|
7
|
+
* @generated by codegen project: GenerateStateH.js
|
|
8
|
+
*/
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
#ifdef ANDROID
|
|
12
|
+
#include <folly/dynamic.h>
|
|
13
|
+
#include <react/renderer/mapbuffer/MapBuffer.h>
|
|
14
|
+
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
namespace facebook {
|
|
18
|
+
namespace react {
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
} // namespace react
|
|
23
|
+
} // namespace facebook
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { TurboModule } from "@rnoh/react-native-openharmony/ts";
|
|
8
|
+
import type { TurboModuleContext } from "@rnoh/react-native-openharmony/ts";
|
|
9
|
+
import { AudioRecordManager } from './AudioRecordManager';
|
|
10
|
+
import { RecordingOptions, PathMap } from './AudioType';
|
|
11
|
+
import { TM } from "./generated/ts";
|
|
12
|
+
|
|
13
|
+
export class AudioModule extends TurboModule implements TM.RTNAudio.Spec {
|
|
14
|
+
ctx!: TurboModuleContext;
|
|
15
|
+
audioRecorderManager: AudioRecordManager = new AudioRecordManager(this.ctx);
|
|
16
|
+
|
|
17
|
+
prepareRecordingAtPath(path: string, options: RecordingOptions): Promise<void> {
|
|
18
|
+
return this.audioRecorderManager.prepareRecordingAtPath(path, options);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
startRecording(): Promise<void> {
|
|
22
|
+
return this.audioRecorderManager.startRecording();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pauseRecording(): Promise<void> {
|
|
26
|
+
return this.audioRecorderManager.pauseRecording();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
resumeRecording(): Promise<void> {
|
|
30
|
+
return this.audioRecorderManager.resumeRecording();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
stopRecording(): Promise<void> {
|
|
34
|
+
return this.audioRecorderManager.stopRecording();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
requestAuthorization(): Promise<boolean> {
|
|
38
|
+
return this.audioRecorderManager.requestAuthorization();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
getAllPath(): PathMap {
|
|
42
|
+
return this.audioRecorderManager.getAllPath();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
checkAuthorizationStatus(): Promise<boolean> {
|
|
46
|
+
return this.audioRecorderManager.checkAuthorizationStatus();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts';
|
|
8
|
+
import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
|
|
9
|
+
import { AudioModule } from './AudioModule';
|
|
10
|
+
import { TM } from "./generated/ts";
|
|
11
|
+
|
|
12
|
+
class AudioModulesFactory extends TurboModulesFactory {
|
|
13
|
+
createTurboModule(name: string): TurboModule | null {
|
|
14
|
+
if (name === TM.RTNAudio.NAME) {
|
|
15
|
+
return new AudioModule(this.ctx)
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
hasTurboModule(name: string): boolean {
|
|
21
|
+
return name === TM.RTNAudio.NAME;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class AudioPackage extends RNPackage {
|
|
26
|
+
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
|
|
27
|
+
return new AudioModulesFactory(ctx);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import media from '@ohos.multimedia.media';
|
|
8
|
+
import { BusinessError } from '@ohos.base';
|
|
9
|
+
import promptAction from '@ohos.promptAction';
|
|
10
|
+
import { RecordingOptions, PathMap } from './AudioType';
|
|
11
|
+
import fs from '@ohos.file.fs';
|
|
12
|
+
import common from '@ohos.app.ability.common';
|
|
13
|
+
import bundleManager from '@ohos.bundle.bundleManager';
|
|
14
|
+
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
|
|
15
|
+
import type { RNOHContext,RNOHLogger } from '@rnoh/react-native-openharmony/ts';
|
|
16
|
+
import { stopWatch } from './StopWatch';
|
|
17
|
+
import util from '@ohos.util';
|
|
18
|
+
|
|
19
|
+
const TIP_BOTTOM = 140;
|
|
20
|
+
const TOAST_DURATION = 1500;
|
|
21
|
+
const PERMISSION_LIST: Array<Permissions> = ['ohos.permission.MICROPHONE'];
|
|
22
|
+
|
|
23
|
+
enum AVRecorderStateEnum {
|
|
24
|
+
IDLE = 'idle',
|
|
25
|
+
PREPARED = 'prepared',
|
|
26
|
+
STARTED = 'started',
|
|
27
|
+
PAUSED = 'paused',
|
|
28
|
+
STOPPED = 'stopped',
|
|
29
|
+
RELEASED = 'released',
|
|
30
|
+
ERROR = 'error'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class AudioRecordManager {
|
|
34
|
+
private context: common.UIAbilityContext
|
|
35
|
+
private ctx!: RNOHContext;
|
|
36
|
+
private avRecorder: media.AVRecorder = {} as media.AVRecorder;
|
|
37
|
+
private isRecording: boolean = false;
|
|
38
|
+
private avProfile: media.AVRecorderProfile = {
|
|
39
|
+
audioBitrate: 100000, //音频比特率
|
|
40
|
+
audioChannels: 2, //音频声道数
|
|
41
|
+
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
|
|
42
|
+
audioSampleRate: 48000, // 音频采样率
|
|
43
|
+
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, //封装格式,当前只支持m4a
|
|
44
|
+
};
|
|
45
|
+
private avConfig: media.AVRecorderConfig = {
|
|
46
|
+
audioSourceType: media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC, //音频输入源,这里设置为麦克风(1)
|
|
47
|
+
profile: this.avProfile,
|
|
48
|
+
url: 'fd://35', //使用fs.openSync()获取文件fd
|
|
49
|
+
};
|
|
50
|
+
private state: media.AVRecorderState = AVRecorderStateEnum.IDLE;
|
|
51
|
+
private timer: number | null = null;
|
|
52
|
+
private file: fs.File;
|
|
53
|
+
private includeBase64: boolean = false;
|
|
54
|
+
private filePath: string = '';
|
|
55
|
+
private logger: RNOHLogger
|
|
56
|
+
|
|
57
|
+
constructor(ctx: RNOHContext) {
|
|
58
|
+
this.ctx = ctx;
|
|
59
|
+
this.context = ctx.uiAbilityContext
|
|
60
|
+
this.logger = ctx.logger.clone("AudioRecorder");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//开始录制对应的配置
|
|
64
|
+
async prepareRecordingAtPath(path: string, options: RecordingOptions): Promise<void> {
|
|
65
|
+
if (this.isRecording) {
|
|
66
|
+
this.logger.error('Please call stopRecording before starting recording.');
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (path === '') {
|
|
70
|
+
this.logger.error('Invalid path.');
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const isAuthorization = await this.checkAuthorizationStatus();
|
|
74
|
+
if (!isAuthorization) {
|
|
75
|
+
this.logger.error('Please obtain microphone authorization first.');
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
//创建录制实例
|
|
80
|
+
this.avRecorder = await media.createAVRecorder();
|
|
81
|
+
//监听状态改变
|
|
82
|
+
this.avRecorder.on('stateChange', (state: media.AVRecorderState) => {
|
|
83
|
+
this.state = state;
|
|
84
|
+
this.logger.info(`current state is ${state}.`);
|
|
85
|
+
})
|
|
86
|
+
//错误上报信息
|
|
87
|
+
this.avRecorder.on('error', (error: BusinessError) => {
|
|
88
|
+
this.logger.error(`AudioRecorder failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
89
|
+
if (error.code === 5400107) {
|
|
90
|
+
this.logger.error(`Please call stopRecording before starting recording.`);
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
//初始化音频参数
|
|
94
|
+
this.avProfile.audioSampleRate = options.SampleRate;
|
|
95
|
+
this.avProfile.audioChannels = options.Channels;
|
|
96
|
+
this.avProfile.audioCodec = this.getAudioCodecFormatString(options.AudioEncoding);
|
|
97
|
+
this.avProfile.audioBitrate = options.AudioEncodingBitRate;
|
|
98
|
+
this.avProfile.fileFormat = this.getFileFormatFormatString(options.OutputFormat);
|
|
99
|
+
this.avConfig.audioSourceType = this.getAudioSourceFormatString(options.AudioSource);
|
|
100
|
+
//获取应用文件路径
|
|
101
|
+
this.filePath = path;
|
|
102
|
+
this.file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
|
|
103
|
+
this.avConfig.url = `fd://${this.file.fd}`;
|
|
104
|
+
this.includeBase64 = options.IncludeBase64;
|
|
105
|
+
await this.avRecorder.prepare(this.avConfig).then((res) => {
|
|
106
|
+
this.logger.info(`${res}.`);
|
|
107
|
+
}).catch((err) => {
|
|
108
|
+
this.logger.error(`${err}.`);
|
|
109
|
+
});
|
|
110
|
+
this.logger.info(`Recording is prepared.`);
|
|
111
|
+
this.logger.debug('Recording is prepared.');
|
|
112
|
+
} catch (error) {
|
|
113
|
+
this.logger.error(`${JSON.stringify(error)}.`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//请求麦克风权限
|
|
118
|
+
async requestAuthorization(): Promise<boolean> {
|
|
119
|
+
let grantStatus: boolean = await this.checkAuthorizationStatus(PERMISSION_LIST[0]);
|
|
120
|
+
let authorizationStatus: boolean = grantStatus;
|
|
121
|
+
if (grantStatus) {
|
|
122
|
+
//已经授权,可以继续访问目标操作
|
|
123
|
+
this.logger.info(`Already authorized.`);
|
|
124
|
+
} else {
|
|
125
|
+
this.logger.info(`No authorization.`);
|
|
126
|
+
await this.requestAuth(PERMISSION_LIST);
|
|
127
|
+
authorizationStatus = await this.checkAuthorizationStatus();
|
|
128
|
+
}
|
|
129
|
+
return authorizationStatus;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
//请求权限
|
|
133
|
+
async requestAuth(permissions: Array<Permissions>) {
|
|
134
|
+
let atManager = abilityAccessCtrl.createAtManager();
|
|
135
|
+
try {
|
|
136
|
+
const data = await atManager.requestPermissionsFromUser(this.context, permissions);
|
|
137
|
+
let grantStatus: Array<number> = data.authResults;
|
|
138
|
+
let length: number = grantStatus.length;
|
|
139
|
+
for (let i = 0; i < length; i++) {
|
|
140
|
+
if (grantStatus[i] === 0) {
|
|
141
|
+
//用户授权,可以继续访问目标操作
|
|
142
|
+
this.logger.info(`Authorization successful.`);
|
|
143
|
+
this.logger.debug('Authorization successful.');
|
|
144
|
+
} else {
|
|
145
|
+
//用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
|
|
146
|
+
this.logger.info(`Deny authorization.`);
|
|
147
|
+
this.logger.debug('Deny authorization! Recording requires authorization. Enter the system settings and turn on microphone permissions.');
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} catch (error) {
|
|
152
|
+
this.logger.error(`requestPermissionsFromUser failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//检查是否授权
|
|
157
|
+
async checkAuthorizationStatus(permission: Permissions = PERMISSION_LIST[0]): Promise<boolean> {
|
|
158
|
+
let atManager = abilityAccessCtrl.createAtManager();
|
|
159
|
+
let grantStatus: abilityAccessCtrl.GrantStatus;
|
|
160
|
+
//获取应用程序的accessTokenID
|
|
161
|
+
let tokenId: number;
|
|
162
|
+
try {
|
|
163
|
+
let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
|
|
164
|
+
let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
|
|
165
|
+
tokenId = appInfo.accessTokenId;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
this.logger.error(`getBundleInfoForSelf failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
168
|
+
}
|
|
169
|
+
//检查应用是否被授予权限
|
|
170
|
+
try {
|
|
171
|
+
grantStatus = await atManager.checkAccessToken(tokenId, permission);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
this.logger.error(`checkAccessToken failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
174
|
+
}
|
|
175
|
+
return grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//格式化音频输入源
|
|
179
|
+
getAudioSourceFormatString(audioSource: number) {
|
|
180
|
+
switch (audioSource) {
|
|
181
|
+
case 0:
|
|
182
|
+
return media.AudioSourceType.AUDIO_SOURCE_TYPE_DEFAULT;
|
|
183
|
+
case 1:
|
|
184
|
+
return media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC;
|
|
185
|
+
default:
|
|
186
|
+
this.logger.debug(`Using media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC : ${media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC}.`);
|
|
187
|
+
return media.AudioSourceType.AUDIO_SOURCE_TYPE_MIC;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
//格式化音频编码格式,当前仅支持aac
|
|
192
|
+
getAudioCodecFormatString(audioEncoding: string) {
|
|
193
|
+
switch (audioEncoding) {
|
|
194
|
+
case 'aac':
|
|
195
|
+
return media.CodecMimeType.AUDIO_AAC;
|
|
196
|
+
default:
|
|
197
|
+
this.logger.debug(`Using media.CodecMimeType.AUDIO_AAC : ${media.CodecMimeType.AUDIO_AAC}.`);
|
|
198
|
+
return media.CodecMimeType.AUDIO_AAC;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//格式化封装格式,当前仅支持m4a
|
|
203
|
+
getFileFormatFormatString(fileFormat: string) {
|
|
204
|
+
switch (fileFormat) {
|
|
205
|
+
case 'm4a':
|
|
206
|
+
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
207
|
+
default:
|
|
208
|
+
this.logger.debug(`Using media.ContainerFormatType.CFT_MPEG_4A : ${media.ContainerFormatType.CFT_MPEG_4A}.`);
|
|
209
|
+
return media.ContainerFormatType.CFT_MPEG_4A;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
//开始录制
|
|
214
|
+
public async startRecording() {
|
|
215
|
+
try {
|
|
216
|
+
if (this.avRecorder.state === AVRecorderStateEnum.STARTED || this.avRecorder.state === AVRecorderStateEnum.PAUSED) {
|
|
217
|
+
this.logger.error('Please call stopRecording before starting recording.');
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (this.avRecorder.state !== AVRecorderStateEnum.PREPARED) {
|
|
221
|
+
this.logger.error('Please call prepareRecording before starting recording.');
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
await this.avRecorder.start();
|
|
225
|
+
this.isRecording = true;
|
|
226
|
+
this.logger.info(`start recording.`);
|
|
227
|
+
this.logger.debug('start recording.');
|
|
228
|
+
stopWatch.reset();
|
|
229
|
+
stopWatch.start();
|
|
230
|
+
this.startTimer();
|
|
231
|
+
} catch (error) {
|
|
232
|
+
this.logger.error(`startRecording failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
//暂停录制
|
|
237
|
+
public async pauseRecording() {
|
|
238
|
+
if (this.avRecorder.state === AVRecorderStateEnum.STARTED) { //仅在started状态下调用pause为合理状态切换
|
|
239
|
+
try {
|
|
240
|
+
await this.avRecorder.pause();
|
|
241
|
+
stopWatch.stop();
|
|
242
|
+
this.logger.debug('pause recording.');
|
|
243
|
+
} catch (error) {
|
|
244
|
+
this.logger.error(`pauseRecording failed, code is ${error?.code}, message is ${error?.message}.`);
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
this.logger.error('It is reasonable to call pauseRecording only in the started state.');
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
//恢复录制
|
|
253
|
+
public async resumeRecording() {
|
|
254
|
+
if (this.avRecorder.state === AVRecorderStateEnum.PAUSED) { //仅在paused状态下调用resume为合理状态切换
|
|
255
|
+
try {
|
|
256
|
+
await this.avRecorder.resume();
|
|
257
|
+
stopWatch.start();
|
|
258
|
+
this.logger.debug('resume recording.');
|
|
259
|
+
} catch (error) {
|
|
260
|
+
this.logger.error(`resumeRecording failed. code is ${error?.code}, message is ${error?.message}.`);
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
this.logger.error('It is reasonable to call resumeRecording only in the paused state.');
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
//停止录制
|
|
269
|
+
public async stopRecording() {
|
|
270
|
+
if (this.avRecorder.state === AVRecorderStateEnum.STARTED || this.avRecorder.state === AVRecorderStateEnum.PAUSED) {
|
|
271
|
+
try {
|
|
272
|
+
//仅在started或者paused状态下调用stop为合理状态切换
|
|
273
|
+
|
|
274
|
+
await this.avRecorder.stop();
|
|
275
|
+
//重置
|
|
276
|
+
await this.avRecorder.reset();
|
|
277
|
+
//释放录制实例
|
|
278
|
+
await this.avRecorder.release();
|
|
279
|
+
this.isRecording = false;
|
|
280
|
+
this.stopTimer();
|
|
281
|
+
stopWatch.stop();
|
|
282
|
+
} catch (error) {
|
|
283
|
+
this.logger.error(`stopRecording failed. code is ${error?.code}, message is ${error?.message}.`);
|
|
284
|
+
} finally {
|
|
285
|
+
fs.closeSync(this.file);
|
|
286
|
+
this.convertM4aToBase64();
|
|
287
|
+
this.logger.debug('stop recording.');
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
this.logger.error('It is reasonable to call stopRecording only in the started or paused state.');
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
//将音频文件转成base64格式
|
|
296
|
+
convertM4aToBase64() {
|
|
297
|
+
let currentTime: number = stopWatch.getTimeSeconds();
|
|
298
|
+
let base64: string = '';
|
|
299
|
+
let stat: fs.Stat = fs.lstatSync(this.filePath);
|
|
300
|
+
let flag: boolean = true;
|
|
301
|
+
if (this.includeBase64) {
|
|
302
|
+
let file: fs.File = fs.openSync(this.filePath, fs.OpenMode.READ_ONLY);
|
|
303
|
+
try {
|
|
304
|
+
let buffer = new ArrayBuffer(stat.size);
|
|
305
|
+
fs.readSync(file.fd, buffer);
|
|
306
|
+
let unit8Array: Uint8Array = new Uint8Array(buffer);
|
|
307
|
+
let base64Helper = new util.Base64Helper();
|
|
308
|
+
base64 = base64Helper.encodeToStringSync(unit8Array, util.Type.BASIC);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
flag = false;
|
|
311
|
+
this.logger.error(`base64Helper encodeToString failed. code is ${error?.code}, message is ${error?.message}.`);
|
|
312
|
+
} finally {
|
|
313
|
+
fs.closeSync(file);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
this.ctx.rnInstance.emitDeviceEvent('recordingFinished', {
|
|
317
|
+
base64,
|
|
318
|
+
duration: currentTime,
|
|
319
|
+
status: flag ? 'OK' : 'ERROR',
|
|
320
|
+
audioFileURL: this.filePath,
|
|
321
|
+
audioFileSize: stat.size
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
//获取存储路径
|
|
326
|
+
public getAllPath(): PathMap {
|
|
327
|
+
const pathMap: PathMap = {
|
|
328
|
+
FilesDirectoryPath: this.context.filesDir,
|
|
329
|
+
CacheDirectoryPath: this.context.cacheDir,
|
|
330
|
+
TempsDirectoryPath: this.context.tempDir,
|
|
331
|
+
}
|
|
332
|
+
this.logger.info(`return the pathMap.`);
|
|
333
|
+
return pathMap;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private startTimer() {
|
|
337
|
+
this.stopTimer();
|
|
338
|
+
this.timer = setInterval(() => {
|
|
339
|
+
if (this.avRecorder.state === AVRecorderStateEnum.STARTED) {
|
|
340
|
+
let currentTime: number = stopWatch.getTimeSeconds();
|
|
341
|
+
this.ctx.rnInstance.emitDeviceEvent('recordingProgress', { currentTime });
|
|
342
|
+
}
|
|
343
|
+
}, 1000)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private stopTimer() {
|
|
347
|
+
if (this.timer !== null) {
|
|
348
|
+
clearInterval(this.timer);
|
|
349
|
+
this.timer = null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
type AudioQuality = 'Low' | 'Medium' | 'High';
|
|
8
|
+
|
|
9
|
+
export interface RecordingOptions {
|
|
10
|
+
SampleRate: number,
|
|
11
|
+
Channels: number,
|
|
12
|
+
AudioQuality?: AudioQuality,
|
|
13
|
+
AudioEncoding: string,
|
|
14
|
+
MeteringEnabled?: boolean,
|
|
15
|
+
MeasurementMode?: boolean,
|
|
16
|
+
AudioEncodingBitRate: number,
|
|
17
|
+
IncludeBase64: boolean,
|
|
18
|
+
OutputFormat: string,
|
|
19
|
+
AudioSource: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PathMap {
|
|
23
|
+
FilesDirectoryPath: string,
|
|
24
|
+
CacheDirectoryPath: string,
|
|
25
|
+
TempsDirectoryPath: string,
|
|
26
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
|
|
3
|
+
* Use of this source code is governed by a MIT license that can be
|
|
4
|
+
* found in the LICENSE file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class StopWatch {
|
|
8
|
+
private startTime: number = 0;
|
|
9
|
+
private elapsedTime: number = 0;
|
|
10
|
+
private paused: boolean = true;
|
|
11
|
+
|
|
12
|
+
public start(): void {
|
|
13
|
+
this.startTime = new Date().getTime();
|
|
14
|
+
this.paused = false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public stop(): number {
|
|
18
|
+
if (!this.paused) {
|
|
19
|
+
let nowTime = new Date().getTime();
|
|
20
|
+
this.elapsedTime += (nowTime - this.startTime) / 1000;
|
|
21
|
+
this.paused = true;
|
|
22
|
+
}
|
|
23
|
+
return this.elapsedTime;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public reset(): void {
|
|
27
|
+
this.startTime = 0;
|
|
28
|
+
this.elapsedTime = 0;
|
|
29
|
+
this.paused = true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public getTimeSeconds(): number {
|
|
33
|
+
let seconds: number = 0;
|
|
34
|
+
if (this.paused) {
|
|
35
|
+
seconds = this.elapsedTime;
|
|
36
|
+
} else {
|
|
37
|
+
let nowTime = new Date().getTime();
|
|
38
|
+
seconds = this.elapsedTime + (nowTime - this.startTime) / 1000;
|
|
39
|
+
}
|
|
40
|
+
return seconds;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const stopWatch = new StopWatch();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-lib-harmony"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Tag } from "@rnoh/react-native-openharmony/ts"
|
|
6
|
+
|
|
7
|
+
export namespace RTNAudio {
|
|
8
|
+
export const NAME = 'RTNAudio' as const
|
|
9
|
+
|
|
10
|
+
export type RecordingOptions = {SampleRate: number, Channels: number, AudioQuality?: string, AudioEncoding: string, MeteringEnabled?: boolean, MeasurementMode?: boolean, AudioEncodingBitRate: number, IncludeBase64: boolean, OutputFormat: string, AudioSource: number}
|
|
11
|
+
|
|
12
|
+
export type PathMap = {FilesDirectoryPath: string, CacheDirectoryPath: string, TempsDirectoryPath: string}
|
|
13
|
+
|
|
14
|
+
export interface Spec {
|
|
15
|
+
prepareRecordingAtPath(path: string, options: RecordingOptions): Promise<void>;
|
|
16
|
+
|
|
17
|
+
requestAuthorization(): Promise<boolean>;
|
|
18
|
+
|
|
19
|
+
startRecording(): Promise<void>;
|
|
20
|
+
|
|
21
|
+
pauseRecording(): Promise<void>;
|
|
22
|
+
|
|
23
|
+
resumeRecording(): Promise<void>;
|
|
24
|
+
|
|
25
|
+
stopRecording(): Promise<void>;
|
|
26
|
+
|
|
27
|
+
getAllPath(): PathMap;
|
|
28
|
+
|
|
29
|
+
checkAuthorizationStatus(): Promise<boolean>;
|
|
30
|
+
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
module: {
|
|
3
|
+
name: 'audio',
|
|
4
|
+
type: 'har',
|
|
5
|
+
deviceTypes: ['default'],
|
|
6
|
+
mainElement: "AudioRecorder",
|
|
7
|
+
requestPermissions: [
|
|
8
|
+
{
|
|
9
|
+
name: 'ohos.permission.MICROPHONE',
|
|
10
|
+
reason: "$string:label_permission_microphone",
|
|
11
|
+
usedScene: {
|
|
12
|
+
abilities: [
|
|
13
|
+
"EntryAbility"
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"string": [
|
|
3
|
+
{
|
|
4
|
+
"name": "page_show",
|
|
5
|
+
"value": "page from npm package"
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
"name": "label_permission_write_media",
|
|
9
|
+
"value": "用于媒体文件写"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"name": "label_permission_read_media",
|
|
13
|
+
"value": "用于媒体文件读"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "label_permission_microphone",
|
|
17
|
+
"value": "用于录制音频"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
|
Binary file
|
package/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import React from "react";
|
|
4
|
+
|
|
5
|
+
import ReactNative, {
|
|
6
|
+
NativeModules,
|
|
7
|
+
DeviceEventEmitter,
|
|
8
|
+
PermissionsAndroid,
|
|
9
|
+
Platform
|
|
10
|
+
} from "react-native";
|
|
11
|
+
|
|
12
|
+
// @ts-ignore We want to check whether __turboModuleProxy exitst, it may not
|
|
13
|
+
const isTurboModuleEnabled = global.__turboModuleProxy != null;
|
|
14
|
+
|
|
15
|
+
const nativeRecorderManager = isTurboModuleEnabled ?
|
|
16
|
+
require("./NativeAudio").default :
|
|
17
|
+
NativeModules.AudioRecorderManager;
|
|
18
|
+
|
|
19
|
+
const AudioRecorder = {
|
|
20
|
+
prepareRecordingAtPath: function (path, options) {
|
|
21
|
+
if (this.progressSubscription) this.progressSubscription.remove();
|
|
22
|
+
this.progressSubscription = DeviceEventEmitter.addListener('recordingProgress',
|
|
23
|
+
(data) => {
|
|
24
|
+
if (this.onProgress) {
|
|
25
|
+
this.onProgress(data);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
if (this.finishedSubscription) this.finishedSubscription.remove();
|
|
31
|
+
this.finishedSubscription = DeviceEventEmitter.addListener('recordingFinished',
|
|
32
|
+
(data) => {
|
|
33
|
+
if (this.onFinished) {
|
|
34
|
+
this.onFinished(data);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const defaultOptions = {
|
|
40
|
+
SampleRate: 48000,
|
|
41
|
+
Channels: 2,
|
|
42
|
+
AudioQuality: 'High',
|
|
43
|
+
AudioEncoding: 'ima4',
|
|
44
|
+
OutputFormat: 'mpeg_4',
|
|
45
|
+
MeteringEnabled: false,
|
|
46
|
+
MeasurementMode: false,
|
|
47
|
+
AudioEncodingBitRate: 100000,
|
|
48
|
+
IncludeBase64: false,
|
|
49
|
+
AudioSource: 0
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const recordingOptions = { ...defaultOptions, ...options };
|
|
53
|
+
|
|
54
|
+
if (Platform.OS === 'ios') {
|
|
55
|
+
nativeRecorderManager.prepareRecordingAtPath(
|
|
56
|
+
path,
|
|
57
|
+
recordingOptions.SampleRate,
|
|
58
|
+
recordingOptions.Channels,
|
|
59
|
+
recordingOptions.AudioQuality,
|
|
60
|
+
recordingOptions.AudioEncoding,
|
|
61
|
+
recordingOptions.MeteringEnabled,
|
|
62
|
+
recordingOptions.MeasurementMode,
|
|
63
|
+
recordingOptions.IncludeBase64
|
|
64
|
+
);
|
|
65
|
+
} else {
|
|
66
|
+
return nativeRecorderManager.prepareRecordingAtPath(path, recordingOptions);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
startRecording: function () {
|
|
70
|
+
return nativeRecorderManager.startRecording();
|
|
71
|
+
},
|
|
72
|
+
pauseRecording: function () {
|
|
73
|
+
return nativeRecorderManager.pauseRecording();
|
|
74
|
+
},
|
|
75
|
+
resumeRecording: function () {
|
|
76
|
+
return nativeRecorderManager.resumeRecording();
|
|
77
|
+
},
|
|
78
|
+
stopRecording: async function () {
|
|
79
|
+
await nativeRecorderManager.stopRecording();
|
|
80
|
+
const timer = setTimeout( ()=>{
|
|
81
|
+
this.removeListeners();
|
|
82
|
+
this.clearCallback();
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
}, 200 );
|
|
85
|
+
},
|
|
86
|
+
checkAuthorizationStatus: nativeRecorderManager.checkAuthorizationStatus,
|
|
87
|
+
requestAuthorization: async () => {
|
|
88
|
+
if (Platform.OS === 'harmony') {
|
|
89
|
+
const res = await nativeRecorderManager.requestAuthorization();
|
|
90
|
+
return res;
|
|
91
|
+
} else {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
PermissionsAndroid.request(
|
|
94
|
+
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO
|
|
95
|
+
).then(result => {
|
|
96
|
+
if (result === PermissionsAndroid.RESULTS.GRANTED || result === true) {
|
|
97
|
+
resolve(true);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
resolve(false);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
getAllPath: function () {
|
|
107
|
+
const res = nativeRecorderManager.getAllPath();
|
|
108
|
+
return res;
|
|
109
|
+
},
|
|
110
|
+
removeListeners: function () {
|
|
111
|
+
if (this.progressSubscription) this.progressSubscription.remove();
|
|
112
|
+
if (this.finishedSubscription) this.finishedSubscription.remove();
|
|
113
|
+
},
|
|
114
|
+
clearCallback: function () {
|
|
115
|
+
if (this.onProgress) {
|
|
116
|
+
this.onProgress = null;
|
|
117
|
+
}
|
|
118
|
+
if (this.onFinished) {
|
|
119
|
+
this.onFinished = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
let AudioUtils = {};
|
|
125
|
+
let AudioSource = {};
|
|
126
|
+
|
|
127
|
+
if (Platform.OS === 'ios') {
|
|
128
|
+
AudioUtils = {
|
|
129
|
+
MainBundlePath: nativeRecorderManager.MainBundlePath,
|
|
130
|
+
CachesDirectoryPath: nativeRecorderManager.NSCachesDirectoryPath,
|
|
131
|
+
DocumentDirectoryPath: nativeRecorderManager.NSDocumentDirectoryPath,
|
|
132
|
+
LibraryDirectoryPath: nativeRecorderManager.NSLibraryDirectoryPath,
|
|
133
|
+
};
|
|
134
|
+
} else if (Platform.OS === 'harmony') {
|
|
135
|
+
const { FilesDirectoryPath, CacheDirectoryPath, TempsDirectoryPath } = AudioRecorder.getAllPath();
|
|
136
|
+
AudioUtils = {
|
|
137
|
+
FilesDirectoryPath,
|
|
138
|
+
CacheDirectoryPath,
|
|
139
|
+
TempsDirectoryPath
|
|
140
|
+
};
|
|
141
|
+
AudioSource = {
|
|
142
|
+
DEFAULT: 0,
|
|
143
|
+
MIC: 1,
|
|
144
|
+
};
|
|
145
|
+
} else if (Platform.OS === 'android') {
|
|
146
|
+
AudioUtils = {
|
|
147
|
+
MainBundlePath: nativeRecorderManager.MainBundlePath,
|
|
148
|
+
CachesDirectoryPath: nativeRecorderManager.CachesDirectoryPath,
|
|
149
|
+
DocumentDirectoryPath: nativeRecorderManager.DocumentDirectoryPath,
|
|
150
|
+
LibraryDirectoryPath: nativeRecorderManager.LibraryDirectoryPath,
|
|
151
|
+
PicturesDirectoryPath: nativeRecorderManager.PicturesDirectoryPath,
|
|
152
|
+
MusicDirectoryPath: nativeRecorderManager.MusicDirectoryPath,
|
|
153
|
+
DownloadsDirectoryPath: nativeRecorderManager.DownloadsDirectoryPath
|
|
154
|
+
};
|
|
155
|
+
AudioSource = {
|
|
156
|
+
DEFAULT: 0,
|
|
157
|
+
MIC: 1,
|
|
158
|
+
VOICE_UPLINK: 2,
|
|
159
|
+
VOICE_DOWNLINK: 3,
|
|
160
|
+
VOICE_CALL: 4,
|
|
161
|
+
CAMCORDER: 5,
|
|
162
|
+
VOICE_RECOGNITION: 6,
|
|
163
|
+
VOICE_COMMUNICATION: 7,
|
|
164
|
+
REMOTE_SUBMIX: 8, // added in API 19
|
|
165
|
+
UNPROCESSED: 9, // added in API 24
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
module.exports = { AudioRecorder, AudioUtils, AudioSource };
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@react-native-ohos/react-native-audio",
|
|
3
|
+
"version": "4.2.3-rc.1",
|
|
4
|
+
"description": "React Native extension for recording audio",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Joshua Sierles <joshua@diluvia.net> (https://github.com/jsierles)",
|
|
7
|
+
"harmony": {
|
|
8
|
+
"alias": "react-native-audio"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"/harmony",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"index.js",
|
|
15
|
+
"NativeAudio.ts"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react-native",
|
|
19
|
+
"audio",
|
|
20
|
+
"record"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://gitee.com/openharmony-sig/rntpc_react-native-audio"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"codegen-lib": "react-native codegen-lib-harmony --no-safety-check --npm-package-name react-native-audio --cpp-output-path ./harmony/audio/src/main/cpp/generated --ets-output-path ./harmony/audio/src/main/ets/generated --turbo-modules-spec-paths ./NativeAudio.ts "
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@rnoh/react-native-harmony-cli": "npm:@react-native-oh/react-native-harmony-cli@^0.0.27"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"registry": "https://registry.npmjs.org/",
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"nativePackage": true
|
|
37
|
+
}
|