diff --git a/demo/app/main-view-model.ts b/demo/app/main-view-model.ts index a5ceddf..76a249c 100644 --- a/demo/app/main-view-model.ts +++ b/demo/app/main-view-model.ts @@ -7,248 +7,271 @@ import * as platform from 'platform'; import {TNSRecorder, TNSPlayer} from 'nativescript-audio'; export class AudioDemo extends Observable { - public isPlaying: boolean; - public isRecording: boolean; - public recordedAudioFile: string; - private recorder; - private player; - private audioSessionId; - private page; - private audioUrls: Array = [ - { name: 'Fight Club', pic: '~/pics/canoe_girl.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/2514.mp3' }, - { name: 'To The Bat Cave!!!', pic: '~/pics/bears.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/17.mp3' }, - { name: 'Marlon Brando', pic: '~/pics/northern_lights.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/47.mp3' } - ]; - - constructor() { - super(); - - this.player = new TNSPlayer(); - this.recorder = new TNSRecorder(); + public isPlaying: boolean; + public isRecording: boolean; + public recordedAudioFile: string; + private recorder; + private player; + private audioSessionId; + private page; + private audioUrls: Array = [ + { name: 'Fight Club', pic: '~/pics/canoe_girl.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/2514.mp3' }, + { name: 'To The Bat Cave!!!', pic: '~/pics/bears.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/17.mp3' }, + { name: 'Marlon Brando', pic: '~/pics/northern_lights.jpeg', url: 'http://www.noiseaddicts.com/samples_1w72b820/47.mp3' } + ]; + private meterInterval: any; + + constructor() { + super(); + + this.player = new TNSPlayer(); + this.recorder = new TNSRecorder(); + } + + public startRecord(args) { + if (TNSRecorder.CAN_RECORD()) { + + var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); + console.log(JSON.stringify(audioFolder)); + + var recorderOptions = { + + filename: `${audioFolder.path}/recording.${app.android ? 'mp3' : 'caf'}`, + + metering: true, + + infoCallback: () => { + console.log(); + }, + + errorCallback: () => { + console.log(); + snackbar.simple('Error recording.'); + } + }; + + + this.recorder.start(recorderOptions).then((result) => { + this.set("isRecording", true); + if (recorderOptions.metering) { + this.initMeter(); + } + }, (err) => { + this.set("isRecording", false); + this.resetMeter(); + alert(err); + }); + } else { + alert("This device cannot record audio."); + } + } + + public stopRecord(args) { + this.recorder.stop().then(() => { + this.set("isRecording", false); + snackbar.simple("Recorder stopped"); + this.resetMeter(); + }, (ex) => { + console.log(ex); + this.set("isRecording", false); + this.resetMeter(); + }); + } + + private initMeter() { + this.resetMeter(); + this.meterInterval = setInterval(() => { + console.log(this.recorder.getMeters()); + }, 500); + } + + private resetMeter() { + if (this.meterInterval) { + clearInterval(this.meterInterval); + this.meterInterval = undefined; } + } + + public getFile(args) { + try { + var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); + var recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`); + console.log(JSON.stringify(recordedFile)); + console.log('recording exists: ' + fs.File.exists(recordedFile.path)); + this.set("recordedAudioFile", recordedFile.path); + } catch (ex) { + console.log(ex); + } + } - public startRecord(args) { - if (TNSRecorder.CAN_RECORD()) { - var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); - console.log(JSON.stringify(audioFolder)); + public playRecordedFile(args) { - var recorderOptions = { + var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); + var recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`); + console.log("RECORDED FILE : " + JSON.stringify(recordedFile)); - filename: `${audioFolder.path}/recording.${app.android ? 'mp3' : 'caf'}`, + var playerOptions = { + audioFile: `~/audio/recording.${this.platformExtension()}`, - infoCallback: () => { - console.log(); - }, + completeCallback: () => { + snackbar.simple("Audio file complete"); + this.set("isPlaying", false); + this.player.dispose().then(() => { + console.log('DISPOSED'); + }, (err) => { + console.log(err); + }); + }, - errorCallback: () => { - console.log(); - snackbar.simple('Error recording.'); - } - }; + errorCallback: () => { + alert('Error callback'); + this.set("isPlaying", false); + }, + infoCallback: () => { + alert('Info callback'); + } + }; - this.recorder.start(recorderOptions).then((result) => { - this.set("isRecording", true); - }, (err) => { - this.set("isRecording", false); - alert(err); - }); - } else { - alert("This device cannot record audio."); - } - } - public stopRecord(args) { - this.recorder.stop().then(() => { - this.set("isRecording", false); - snackbar.simple("Recorder stopped"); - }, (ex) => { - console.log(ex); - this.set("isRecording", false); - }); - } + this.player.playFromFile(playerOptions).then(() => { + this.set("isPlaying", true); + }, (err) => { + console.log(err); + this.set("isPlaying", false); + }); - public getFile(args) { - try { - var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); - var recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`); - console.log(JSON.stringify(recordedFile)); - console.log('recording exists: ' + fs.File.exists(recordedFile.path)); - this.set("recordedAudioFile", recordedFile.path); - } catch (ex) { - console.log(ex); - } - } + } - public playRecordedFile(args) { - var audioFolder = fs.knownFolders.currentApp().getFolder("audio"); - var recordedFile = audioFolder.getFile(`recording.${this.platformExtension()}`); - console.log("RECORDED FILE : " + JSON.stringify(recordedFile)); + /***** AUDIO PLAYER *****/ - var playerOptions = { - audioFile: `~/audio/recording.${this.platformExtension()}`, + public playAudio(filepath: string, fileType: string) { - completeCallback: () => { - snackbar.simple("Audio file complete"); - this.set("isPlaying", false); - this.player.dispose().then(() => { - console.log('DISPOSED'); - }, (err) => { - console.log(err); - }); - }, + try { + var playerOptions = { + audioFile: filepath, - errorCallback: () => { - alert('Error callback'); - this.set("isPlaying", false); - }, + completeCallback: () => { + snackbar.simple("Audio file complete"); - infoCallback: () => { - alert('Info callback'); - } - }; + this.player.dispose().then(() => { + this.set("isPlaying", false); + console.log('DISPOSED'); + }, (err) => { + console.log('ERROR disposePlayer: ' + err); + }); + }, + + errorCallback: (err) => { + snackbar.simple('Error occurred during playback.'); + console.log(err); + this.set("isPlaying", false); + }, + + infoCallback: (info) => { + alert('Info callback: ' + info.msg); + console.log("what: " + info); + } + }; + this.set("isPlaying", true); + if (fileType === 'localFile') { this.player.playFromFile(playerOptions).then(() => { - this.set("isPlaying", true); + this.set("isPlaying", true); }, (err) => { - console.log(err); - this.set("isPlaying", false); + console.log(err); + this.set("isPlaying", false); }); - + } else if (fileType === 'remoteFile') { + this.player.playFromUrl(playerOptions).then(() => { + this.set("isPlaying", true); + }, (err) => { + console.log(err); + this.set("isPlaying", false); + }); + } + } catch (ex) { + console.log(ex); } + } - /***** AUDIO PLAYER *****/ - - public playAudio(filepath: string, fileType: string) { - - try { - var playerOptions = { - audioFile: filepath, - - completeCallback: () => { - snackbar.simple("Audio file complete"); - - this.player.dispose().then(() => { - this.set("isPlaying", false); - console.log('DISPOSED'); - }, (err) => { - console.log('ERROR disposePlayer: ' + err); - }); - }, - - errorCallback: (err) => { - snackbar.simple('Error occurred during playback.'); - console.log(err); - this.set("isPlaying", false); - }, - - infoCallback: (info) => { - alert('Info callback: ' + info.msg); - console.log("what: " + info); - } - }; - - this.set("isPlaying", true); - - if (fileType === 'localFile') { - this.player.playFromFile(playerOptions).then(() => { - this.set("isPlaying", true); - }, (err) => { - console.log(err); - this.set("isPlaying", false); - }); - } else if (fileType === 'remoteFile') { - this.player.playFromUrl(playerOptions).then(() => { - this.set("isPlaying", true); - }, (err) => { - console.log(err); - this.set("isPlaying", false); - }); - } - } catch (ex) { - console.log(ex); - } - } + ///** + // * PLAY RESOURCES FILE + // */ + // public playResFile(args) { + // var filepath = 'in_the_night'; + // this.playAudio(filepath, 'resFile'); + // } - ///** - // * PLAY RESOURCES FILE - // */ - // public playResFile(args) { - // var filepath = 'in_the_night'; + /** + * PLAY REMOTE AUDIO FILE + */ + public playRemoteFile(args) { + console.log('playRemoteFile'); + var filepath = 'http://www.noiseaddicts.com/samples_1w72b820/2514.mp3'; - // this.playAudio(filepath, 'resFile'); + this.playAudio(filepath, 'remoteFile'); - // } + } - /** - * PLAY REMOTE AUDIO FILE - */ - public playRemoteFile(args) { - console.log('playRemoteFile'); - var filepath = 'http://www.noiseaddicts.com/samples_1w72b820/2514.mp3'; + /** + * PLAY LOCAL AUDIO FILE from app folder + */ + public playLocalFile(args) { + var filepath = '~/audio/angel.mp3'; - this.playAudio(filepath, 'remoteFile'); + this.playAudio(filepath, 'localFile'); - } + } - /** - * PLAY LOCAL AUDIO FILE from app folder - */ - public playLocalFile(args) { - var filepath = '~/audio/angel.mp3'; - this.playAudio(filepath, 'localFile'); - } + /** + * PAUSE PLAYING + */ + public pauseAudio(args) { + this.player.pause().then(() => { + this.set("isPlaying", false); + }, (err) => { + console.log(err); + this.set("isPlaying", true); + }); + } - /** - * PAUSE PLAYING - */ - public pauseAudio(args) { - this.player.pause().then(() => { - this.set("isPlaying", false); - }, (err) => { - console.log(err); - this.set("isPlaying", true); - }); - } + public stopPlaying(args) { + this.player.dispose().then(() => { + snackbar.simple("Media Player Disposed"); + }, (err) => { + console.log(err); + }); + } + /** + * RESUME PLAYING + */ + public resumePlaying(args) { + console.log('START'); + this.player.start(); + } - public stopPlaying(args) { - this.player.dispose().then(() => { - snackbar.simple("Media Player Disposed"); - }, (err) => { - console.log(err); - }); - } - - - /** - * RESUME PLAYING - */ - public resumePlaying(args) { - console.log('START'); - this.player.start(); - } - - private platformExtension() { - return `${app.android ? 'mp3' : 'caf'}`; - } + private platformExtension() { + return `${app.android ? 'mp3' : 'caf'}`; + } } diff --git a/demo/package.json b/demo/package.json index 578a856..4942f7c 100644 --- a/demo/package.json +++ b/demo/package.json @@ -2,10 +2,10 @@ "nativescript": { "id": "org.nativescript.audio", "tns-ios": { - "version": "2.1.1" + "version": "2.2.1" }, "tns-android": { - "version": "2.1.1" + "version": "2.2.0" } }, "dependencies": { @@ -23,4 +23,4 @@ "tns-platform-declarations": "^2.0.0", "typescript": "^1.8.10" } -} +} \ No newline at end of file diff --git a/package.json b/package.json index cd5feb8..16c25c6 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "typings": "index.d.ts", "nativescript": { "platforms": { - "android": "2.1.1", - "ios": "2.1.1" + "android": "2.2.0", + "ios": "2.2.1" } }, "scripts": { diff --git a/src/android/recorder.ts b/src/android/recorder.ts index 41248ed..2cdaf42 100644 --- a/src/android/recorder.ts +++ b/src/android/recorder.ts @@ -54,6 +54,14 @@ export class TNSRecorder implements TNSRecordI { }); } + public getMeters(): number { + if (this.recorder != null) + return this.recorder.getMaxAmplitude(); + else + return 0; + + } + public stop(): Promise { return new Promise((resolve, reject) => { try { diff --git a/src/ios/recorder.ts b/src/ios/recorder.ts index d531b4c..6b141ed 100644 --- a/src/ios/recorder.ts +++ b/src/ios/recorder.ts @@ -41,6 +41,9 @@ export class TNSRecorder extends NSObject implements TNSRecordI { console.log(errorRef.value); } else { this._recorder.delegate = this; + if (options.metering) { + this._recorder.meteringEnabled = true; + } this._recorder.prepareToRecord(); this._recorder.record(); resolve(); @@ -84,17 +87,15 @@ export class TNSRecorder extends NSObject implements TNSRecordI { } public isRecording() { - var _this = this; - return _this._recorder.recording; + return this._recorder.recording; } public getMeters(channel: number) { - var _this = this; - if(!_this._recorder.meteringEnabled) { - _this._recorder.meteringEnabled = true; + if(!this._recorder.meteringEnabled) { + this._recorder.meteringEnabled = true; } - _this._recorder.updateMeters(); - return _this._recorder.averagePowerForChannel(channel); + this._recorder.updateMeters(); + return this._recorder.averagePowerForChannel(channel); } diff --git a/src/options.ts b/src/options.ts index 4eca1f6..4b64a24 100644 --- a/src/options.ts +++ b/src/options.ts @@ -34,6 +34,11 @@ export interface AudioRecorderOptions { */ maxDuration?: number; + /** + * Enable metering. Off by default. + */ + metering?: boolean; + /** * Gets or sets the callback when an error occurs with the media recorder. */