Skip to content

Instantly share code, notes, and snippets.

@vakrilov
Created September 17, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vakrilov/b41e7693c6b245df56185ae5243070ed to your computer and use it in GitHub Desktop.
Save vakrilov/b41e7693c6b245df56185ae5243070ed to your computer and use it in GitHub Desktop.
Noise detection demo
<StackLayout class="page" rows="auto, auto, *">
<Button text="start" (tap)="start()"></Button>
<Button text="stop" (tap)="stop()"></Button>
<Label text="HEYA" class="h2 text-center" [scaleX]="scale" [scaleY]="scale" margin="100"></Label>
</StackLayout>
import { Component } from "@angular/core";
import { hasPermission, requestPermission } from "nativescript-permissions";
@Component({
selector: "ns-items",
moduleId: module.id,
templateUrl: "./items.component.html",
})
export class ItemsComponent {
intervalId;
mRecorder: android.media.MediaRecorder;
scale = 1;
start() {
const audioPermission = (<any>android).Manifest.permission.RECORD_AUDIO;
if (hasPermission(audioPermission)) {
this.record();
} else {
requestPermission(audioPermission, "Can I use the mic ... pls?").then(() => {
this.record()
});
}
}
record() {
this.mRecorder = new android.media.MediaRecorder();
this.mRecorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC);
this.mRecorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.THREE_GPP);
this.mRecorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.AMR_NB);
this.mRecorder.setOutputFile("/dev/null");
this.mRecorder.prepare();
this.mRecorder.start();
this.intervalId = setInterval(() => {
let amplitude = this.mRecorder.getMaxAmplitude();
this.scale = Math.max(1, Math.log10(amplitude));
console.log("Value: " + this.scale);
}, 20);
}
stop() {
clearInterval(this.intervalId);
this.mRecorder.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment