Created
September 17, 2017 08:42
-
-
Save vakrilov/b41e7693c6b245df56185ae5243070ed to your computer and use it in GitHub Desktop.
Noise detection demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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