Template version: v0.2.2
[!TIP] Github address
Find the matching version information in the release address of a third-party library and download an applicable .tgz package: @react-native-oh-tpl/react-native-audio Releases.
Go to the project directory and execute the following instruction:
[!TIP] Replace the content with the path of the .tgz package at the comment sign (#).
npm install @react-native-oh-tpl/react-native-audio@file:#yarn add @react-native-oh-tpl/react-native-audio@file:#The following code shows the basic use scenario of the repository:
[!WARNING] The name of the imported repository remains unchanged.
import { AudioRecorder, AudioUtils } from "@react-native-oh-tpl/react-native-audio";
// request microphone premission
AudioRecorder.requestAuthorization().then((isAuthorised)=>{
console.log(`isAuthorised: ${isAuthorised}`)
})
// you check premission any time
AudioRecorder.checkAuthorizationStatus().then((hasPermission)=>{
console.log(`hasPermission: ${hasPermission}`)
})
// Initialize recording parameters
const path = `${AudioUtils.FilesDirectoryPath}/demo.m4a`
AudioRecorder.prepareRecordingAtPath(path,{
SampleRate: 48000,
Channels: 2,
AudioQuality:'High',//only ios
AudioEncoding: 'aac',
AudioEncodingBitRate: 100000,
AudioSource: 1,
OutputFormat: 'm4a',
MeteringEnabled:false,//only ios
MeasurementMode:false,//only ios
IncludeBase64:false
})
// start recording
AudioRecorder.start();
// pause recording
AudioRecorder.pause();
// resume recording
AudioRecorder.resume();
// stop recording
AudioRecorder.stop();
// add function onProgress
AudioRecorder.onProgress = (data) => {
console.log(data.currentTime)
}
// add function onFinished
AudioRecorder.onFinished = (data) => {
console.log(data.base64);//base64
console.log(data.duration);//audio duration
console.log(data.status);//OK/REEOR
console.log(data.audioFileSize);//audioFileSize
console.log(data.audioFileURL);//audio file url
}If this repository has been adapted to Codegen, generate the bridge code of the third-party library by using the Codegen. For details, see Codegen Usage Guide.
Currently, HarmonyOS does not support AutoLink. Therefore, you need to manually configure the linking.
Open the harmony directory of the HarmonyOS project in DevEco Studio.
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}Currently, two methods are available:
Method 1 (recommended): Use the HAR file.
[!TIP] The HAR file is stored in the
harmonydirectory in the installation path of the third-party library.
Open entry/oh-package.json5 file and add the following dependencies:
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-oh-tpl/react-native-audio": "file:../../node_modules/@react-native-oh-tpl/react-native-audio/harmony/audio.har"
}Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm installMethod 2: Directly link to the source code.
[!TIP] For details, see Directly Linking Source Code.
Open the entry/src/main/ets/RNPackagesFactory.ts file and add the following code:
...
+ import {AudioPackage} from '@react-native-oh-tpl/react-native-audio/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new AudioPackage(ctx)
];
}Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm installThen build and run the code.
This document is verified based on the following versions:
react-native-harmony:0.72.20; SDK:HarmonyOS NEXT Developer Beta1; IDE:DevEco Studio 5.0.3.200; ROM:3.0.0.18;
Check the release version information in the release address of the third-party library: react-native-audio Releases
[!tip] The Platform column indicates the platform where the properties are supported in the original third-party library.
[!tip] If the value of HarmonyOS Support is yes, it means that the HarmonyOS platform supports this property; no means the opposite; partially means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| requestAuthorization | Request microphone permission | function | no | Android/IOS | yes |
| checkAuthorizationStatus | Check authorization status | function | no | Android/IOS | yes |
| prepareRecordingAtPath | Initialize recording instance parameters | function | no | Android/IOS | yes |
| startRecording | start recording | function | no | Android/IOS | yes |
| pauseRecording | Pause recording | function | no | Android/IOS | yes |
| resumeRecording | Resume recording | function | no | Android/IOS | yes |
| stopRecording | stop recording | function | no | Android/IOS | yes |
Encodings supported on iOS: lpcm, ima4, aac, MAC3, MAC6, ulaw, alaw, mp1, mp2, alac, amr.
Encodings supported on Android: aac, aac_eld, amr_nb, amr_wb, he_aac, vorbis.
Encodings supported on Harmony: aac.
AudioQuality、MeteringEnabled、MeasurementMode is only for ios now.
This project is licensed under The MIT License (MIT), Please enjoy and participate freely in open source.