Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nstudio/nativescript-audio-recorder
NativeScript plugin for recording audio.
https://github.com/nstudio/nativescript-audio-recorder
Last synced: 5 days ago
JSON representation
NativeScript plugin for recording audio.
- Host: GitHub
- URL: https://github.com/nstudio/nativescript-audio-recorder
- Owner: nstudio
- License: apache-2.0
- Created: 2019-07-07T20:04:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-07-08T02:34:16.000Z (over 5 years ago)
- Last Synced: 2024-04-13T17:13:23.899Z (7 months ago)
- Language: TypeScript
- Size: 2.57 MB
- Stars: 0
- Watchers: 6
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
NativeScript plugin to record audio on Android and iOS.
Do you need assistance on your project or plugin? Contact the nStudio team anytime at [email protected] to get up to speed with the best practices in mobile and web app development.
---
## Installation
`tns plugin add @nstudio/nativescript-audio-recorder`
## Usage
```typescript
import {
AudioRecorder,
AudioRecorderOptions
} from '@nstudio/nativescript-audio-recorder';
import { File, knownFolders } from 'tns-core-modules/file-system';
import { isAndroid } from 'tns-core-modules/platform';export class SomeClassInYourProject {
private _recorder: AudioRecorder;constructor() {
this._recorder = new AudioRecorder();
}public startRecordingAudio() {
if (!AudioRecorder.DEVICE_CAN_RECORD()) {
console.log('crud, this device cannot record audio');
return;
}const audioFolder = knownFolders.currentApp().getFolder('audio');
let androidFormat;
let androidEncoder;
if (isAndroid) {
androidFormat = android.media.MediaRecorder.OutputFormat.MPEG_4;
androidEncoder = android.media.MediaRecorder.AudioEncoder.AAC;
}const recorderOptions: AudioRecorderOptions = {
filename: `${audioFolder.path}/recording.${isAndroid ? 'm4a' : 'caf'}`,format: androidFormat,
encoder: androidEncoder,
metering: true,
infoCallback: infoObject => {
console.log(JSON.stringify(infoObject));
},errorCallback: errorObject => {
console.log(JSON.stringify(errorObject));
}
};this._recorder
.start(recorderOptions)
.then(result => {
console.log('recording has started', result);
})
.catch(err => {
console.log('oh no, something is wrong and recording did not start');
});
}public pauseRecording() {
this._recorder
.pause()
.then(result => {
console.log('recording has been paused');
})
.catch(err => {
console.log('recording could not be paused');
});
}public async stopRecording() {
const stopResult = await this._recorder.stop().catch(err => {
console.log('oh no recording did not stop correctly');
});
if (stopResult) {
console.log('recording stopped successfully.');
}
}public getFile() {
try {
const audioFolder = knownFolders.currentApp().getFolder('audio');
const recordedFile = audioFolder.getFile(
`recording.${isAndroid ? 'm4a' : 'caf'}`
);
console.log(JSON.stringify(recordedFile));
console.log('recording exists: ' + File.exists(recordedFile.path));
this.recordedAudioFile = recordedFile.path;
} catch (ex) {
console.log(ex);
}
}public async disposeRecorder() {
const disposeResult = await this._recorder.dispose().catch(err => {
dialogs.alert({
message: `Dispose Error: ${err}`,
okButtonText: 'Okay'
});
});
console.log('disposeResult', disposeResult);
this._recorder = new AudioRecorder();
}
}
```## API
| Method | Description |
| ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `start(options: AudioRecorderOptions):Promise` | Starts recording audio from the device. Permissions are required to record. The plugin attempts to request needed permissions. |
| `stop():Promise` | Stops the recording. |
| `pause():Promise` | Pauses the recording. |
| `resume():Promise` | Resumes the recording. |
| `dispose():Promise` | Disposes of the audio recorder. This will release resources and null out the internal recorder. So it's best to create a new instance of the `AudioRecorder` after if you want to record again. |## Interfaces
#### AudioRecorderOptions
```typescript
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.
*/
maxDuration?: number;/**
* Enable metering. Off by default.
*/
metering?: boolean;/**
* Format
*/
format?: any;/**
* Channels
*/
channels?: any;/**
* Sampling rate
*/
sampleRate?: any;/**
* Bit rate
*/
bitRate?: any;/**
* Encoding
*/
encoder?: any;/**
* 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;
}
```## License
Apache License Version 2.0, January 2004