@voicethread/nativescript-audio-recorder
Nativescript 应用程序的音频录制插件。
npm i --save @voicethread/nativescript-audio-recorder

@voicethread/nativescript-audio-recorder

Nativescript 音频录制 apple android

npm

此插件为 Android 和 iOS 提供音频录制功能,支持从设备麦克风输入录制音频并将其保存为 MP4/AAC 音频文件。它还提供了一个将多个音频录制合并在一起的功能。

内容

安装

npm install @voicethread/nativescript-audio-recorder --save

或者

ns plugin add @voicethread/nativescript-audio-recorder

使用

了解如何使用此插件的最佳方式是研究此存储库中包含的演示应用程序。您可以通过查看 apps/demo/src/plugin-demos/nativescript-audio-recorder.ts 来了解如何在 TypeScript 应用程序中使用此插件。

  1. 导入插件。
import { AudioRecorder, AudioRecorderOptions } from '@voicethread/nativescript-audio-recorder';
  1. 录制音频文件。
this.recorder = new AudioRecorder();
//you can tie into events for updating control states
this.recorder.on(AudioRecorder.stoppedEvent, () => {
console.log('audio recording stopped');
});
this.recorder.on(AudioRecorder.completeEvent, (event: AudioRecorderEventData) => {
console.log('audio recording completed, file: ', event.data);
});
this.recorder.on(AudioRecorder.startedEvent, () => {
console.log('audio recording started');
});
this.recorder.on(AudioRecorder.errorEvent, (event: AudioRecorderEventData) => {
console.log('audio recording error!', event.data);
});
let recOptions: AudioRecorderOptions = {
filename: path.join(knownFolders.documents().path, 'audiorecording-1.mp4');,
infoCallback: infoObject => {
console.log('AudioRecorder infoCallback: ', JSON.stringify(infoObject));
},
errorCallback: errorObject => {
console.error('AudioRecorder errorCallback: ', JSON.stringify(errorObject));
},
};
try {
this.recorder
.record(recOptions)
.then(() => {
console.log('recording audio started');
})
.catch(err => {
console.error(err);
});
} catch (err) {
alert(err?.message);
}
  1. (可选) 将多个录制合并为单个文件。
let audioFiles: [string] = ['PATH/TO/audiorecording-1.mp4','PATH/TO/audiorecording-2.mp4'];
let finalfile = await this.recorder.mergeAudioFiles(this.audioFiles, 'PATH/TO/audiorecording.mp4');

Android 特定内容

为了录制音频,您需要将以下权限添加到您的 AndroidManifest.xml 中

<manifest ... >
<uses-permission android:name="android.permission.RECORD_AUDIO" />
...
</manifest>

在演示应用程序中请求权限,我们使用 @nativescript-community perms 插件

iOS 特定内容

为了录制音频,iOS 需要权限访问麦克风。将以下内容添加到您的 Info.plist 中

<key>NSMicrophoneUsageDescription</key>
<string>This app requires access to your microphone to record audio</string>

如果您的应用程序未提供关于为什么需要此权限的描述,可能会被 Apple App Store 拒绝。

注意:如果您在生产应用程序中使用 perms 插件,请首先阅读其 README.md,因为在生产应用程序中使用此插件将需要您添加所有 iOS Info.plist 权限字符串,以避免由于插件包含所有权限类型的代码而被自动处理拒绝。

支持的音频录制选项

export interface AudioRecorderOptions {
/**
* Gets or sets the recorded file name.
*/

filename: string;

/**
* Sets the source for recording ***ANDROID ONLY for now ***
*/

source?: any;

/**
* Gets or set the max duration of the recording session.
* Input in milliseconds, which is Android's format.
* Will be converted appropriately for iOS.
*/

maxDuration?: number;

/**
* Enable metering. Off by default.
*/

metering?: boolean;

/**
* Channels
*/

channels?: any;

/**
* Sampling rate
*/

sampleRate?: any;

/**
* Bit rate
*/

bitRate?: any; //Android only, use iosAudioQuality for iOS

/**
* Sets the ios audio quality setting. Options are Min|Low|Medium|High|Max. Set to Medium by default.
*/

iosAudioQuality?: string;

/**
* Gets or sets the callback when an error occurs with the media recorder.
* @returns {Object} An object containing the native values for the error callback.
*/

errorCallback?: Function;

/**
* Gets or sets the callback to be invoked to communicate some info and/or warning about the media or its playback.
* @returns {Object} An object containing the native values for the info callback.
*/

infoCallback?: Function;
}

音频录制导出

export class AudioRecorder extends Observable implements IAudioRecorder {
readonly ios: any; //Native iOS recorder instance
readonly android: any; //Native Android recorder instance

/**
* Starts a recording session with the provided options.
* @param options [AudioRecorderOptions]
*/

record(options: AudioRecorderOptions): Promise<any>;

/**
* Stops the recording
*/

stop(): Promise<File>;

/**
* Disposes of the recorder session
*/

dispose(): Promise<any>;

/**
* Returns the maximum absolute amplitude that was sampled since the last call to this method.
* @param channel [number]
*/

getMeters(channel?: number): any;

/**
* Returns value indicating the recorder is currently recording.
*/

isRecording(): boolean;

/**
* Merges all files with file paths specified in audioFiles into a new file at outputPath. This only supports MP4/AAC audio files currently
* Note: For Android, API 26+ is required.
*/

mergeAudioFiles(audioFiles: string[], outputPath: string): Promise<File>;
/**
* Events
*/

public static startedEvent = 'startedEvent';
public static stoppedEvent = 'stoppedEvent';
public static completeEvent = 'completeEvent'; //will pass the recording filename
public static errorEvent = 'errorEvent'; //will pass the error object or string
}

辅助工具

  /*
* Utility to find the duration in milliseconds of the mp4 file at `mp4Path`
*/

export function getDuration(mp4Path: string): number;

在 Android API 25-33 上测试并正常工作。在 iOS 12.x-16.x 上测试并正常工作。

致谢

此插件基于 https://github.com/nstudio/nativescript-audio

许可协议

Apache 许可证版本 2.0