npm i --save @angelengineering/audio-recorder
- 版本:1.2.0
- GitHub: https://github.com/AngelEngineering/nativescript-plugins
- NPM: https://npmjs.net.cn/package/%40angelengineering%2Faudio-recorder
- 下载量
- 昨天:2
- 上周:4
- 上个月:9
@angelengineering/audio-recorder
Nativescript音频录制
此插件为Android和iOS提供了音频录制功能,支持从设备麦克风输入录制音频并保存为MP4/AAC音频文件。它还提供将多个音频录制合并在一起的功能。
内容
安装
npm install @angelengineering/audio-recorder --save
或者
ns plugin add @angelengineering/audio-recorder
使用方法
了解此插件的最佳方式是研究此存储库中包含的演示应用程序。您可以通过查看apps/demo/src/plugin-demos/audio-recorder.ts
来了解如何在TypeScript应用程序中使用此插件。
- 导入插件。
import { AudioRecorder, AudioRecorderOptions } from '@angelengineering/audio-recorder';
- 录制音频文件。
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);
}
- (可选)将多个录音合并为单个文件。
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特定
注意:如果您想合并音频片段,则此插件只能在API26+设备上运行。API 25或以下仅支持单个文件录制。
为了录制音频,您需要将以下权限添加到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>
如果您不提供此权限的原因描述,则您的应用程序可能会被苹果App Store拒绝。
注意:如果您在生产应用程序中使用perms插件,请首先阅读他们的README.md,因为在使用此插件的生产应用程序中,您需要添加所有iOS Info.plist权限字符串,以避免由于插件包含所有权限类型的代码而被自动处理拒绝。
对于iOS,如果设备有多个音频输入设备可用,则插件将首先尝试选择已连接的蓝牙或AirPods设备,然后是耳机,最后是设备麦克风。
支持的音频录制选项
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 the native audio recording control.
* @method record
* @param options AudioRecorderOptions to use when recording audio
* @returns Promise that resolves once recording is complete, or rejects if fails
*/
record(options: AudioRecorderOptions): Promise<void>;
/**
* Stops the native audio recording control.
* @method stop
* @returns Promise that resolves once recording is complete and file has been written, or rejects if fails
*/
stop(): Promise<File>;
/**
* Releases resources from the recorder.
* @method dispose
* @returns Promise that resolves once recorder has been released and disposed, or rejects if fails
*/
dispose(): Promise<void>;
/**
* For Android, returns the maximum absolute amplitude (unsigned 16-bit integer values from 0-32767 ) that was sampled since the last call to this method. Call this only after the setAudioSource().
* For iOS, returns the average power, in decibels full-scale (dBFS), for an audio channel.
* @param channel [number] iOS-only
*/
getMeters(channel?: number): number;
/**
* Returns true if the audio recorder is currently recording, false if not
* @method isRecording
*/
isRecording(): boolean;
/**
* Merges the mp4 files specified by audioFileUrls (array of file paths) into an mp4 audio file
* at the outputPath.
* NOTE: inputs must all be AAC encoded MP4 audio files!
* @method mergeAudioFiles
* @param audioFileUrls
* @param outputPath
**/
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`
* @function getDuration
* @param mp4Path string with the path of the audio file to examine
*/
export function getDuration(mp4Path: string): number;
在Android API 25-33上进行了测试并正常工作。(合并工具仅在26+上工作)在iOS 12.x-16.x上进行了测试并正常工作。
鸣谢
此插件基于https://github.com/nstudio/nativescript-audio
许可证
Apache许可证版本2.0