1
- import "./async-await" ;
2
- import * as app from "tns-core-modules/application" ;
3
- import * as color from "tns-core-modules/color" ;
4
- import * as platform from "tns-core-modules/platform" ;
5
- import * as dialogs from "tns-core-modules/ui/dialogs" ;
6
- import * as timer from "tns-core-modules/timer" ;
7
- import { Observable } from "tns-core-modules/data/observable" ;
8
- import { knownFolders , File } from "tns-core-modules/file-system" ;
9
- import { Page } from "tns-core-modules/ui/page" ;
10
- import { Slider } from "tns-core-modules/ui/slider" ;
11
- import {
12
- TNSRecorder ,
13
- TNSPlayer ,
14
- AudioPlayerOptions ,
15
- AudioRecorderOptions
16
- } from "nativescript-audio" ;
1
+ import { AudioPlayerOptions , AudioRecorderOptions , TNSPlayer , TNSRecorder } from 'nativescript-audio' ;
2
+ import * as app from 'tns-core-modules/application' ;
3
+ import { Observable } from 'tns-core-modules/data/observable' ;
4
+ import { File , knownFolders } from 'tns-core-modules/file-system' ;
5
+ import * as platform from 'tns-core-modules/platform' ;
6
+ import * as timer from 'tns-core-modules/timer' ;
7
+ import * as dialogs from 'tns-core-modules/ui/dialogs' ;
8
+ import { Page } from 'tns-core-modules/ui/page' ;
9
+ import { Slider } from 'tns-core-modules/ui/slider' ;
10
+ import './async-await' ;
17
11
18
12
declare const android ;
19
13
20
14
export class AudioDemo extends Observable {
21
15
@ObservableProperty ( ) public isPlaying : boolean ;
22
16
@ObservableProperty ( ) public isRecording : boolean ;
23
- @ObservableProperty ( ) public audioMeter = "0" ;
17
+ @ObservableProperty ( ) public audioMeter = '0' ;
24
18
@ObservableProperty ( ) public recordedAudioFile : string ;
25
19
@ObservableProperty ( ) public currentVolume ;
26
20
@ObservableProperty ( ) public audioTrackDuration ;
@@ -31,19 +25,19 @@ export class AudioDemo extends Observable {
31
25
private _page ;
32
26
private _audioUrls : Array < any > = [
33
27
{
34
- name : " Fight Club" ,
35
- pic : " ~/pics/canoe_girl.jpeg" ,
36
- url : " http://www.noiseaddicts.com/samples_1w72b820/2514.mp3"
28
+ name : ' Fight Club' ,
29
+ pic : ' ~/pics/canoe_girl.jpeg' ,
30
+ url : ' http://www.noiseaddicts.com/samples_1w72b820/2514.mp3'
37
31
} ,
38
32
{
39
- name : " To The Bat Cave!!!" ,
40
- pic : " ~/pics/bears.jpeg" ,
41
- url : " http://www.noiseaddicts.com/samples_1w72b820/17.mp3"
33
+ name : ' To The Bat Cave!!!' ,
34
+ pic : ' ~/pics/bears.jpeg' ,
35
+ url : ' http://www.noiseaddicts.com/samples_1w72b820/17.mp3'
42
36
} ,
43
37
{
44
- name : " Marlon Brando" ,
45
- pic : " ~/pics/northern_lights.jpeg" ,
46
- url : " http://www.noiseaddicts.com/samples_1w72b820/47.mp3"
38
+ name : ' Marlon Brando' ,
39
+ pic : ' ~/pics/northern_lights.jpeg' ,
40
+ url : ' http://www.noiseaddicts.com/samples_1w72b820/47.mp3'
47
41
}
48
42
] ;
49
43
private _meterInterval : any ;
@@ -58,11 +52,11 @@ export class AudioDemo extends Observable {
58
52
this . _recorder . debug = true ; // set true for tns_recorder logs
59
53
60
54
this . currentVolume = 1 ;
61
- this . _slider = page . getViewById ( " volumeSlider" ) as Slider ;
55
+ this . _slider = page . getViewById ( ' volumeSlider' ) as Slider ;
62
56
63
57
// Set player volume
64
58
if ( this . _slider ) {
65
- this . _slider . on ( " valueChange" , ( data : any ) => {
59
+ this . _slider . on ( ' valueChange' , ( data : any ) => {
66
60
this . _player . volume = this . _slider . value / 100 ;
67
61
} ) ;
68
62
}
@@ -71,10 +65,10 @@ export class AudioDemo extends Observable {
71
65
public async startRecord ( args ) {
72
66
try {
73
67
if ( ! TNSRecorder . CAN_RECORD ( ) ) {
74
- dialogs . alert ( " This device cannot record audio." ) ;
68
+ dialogs . alert ( ' This device cannot record audio.' ) ;
75
69
return ;
76
70
}
77
- const audioFolder = knownFolders . currentApp ( ) . getFolder ( " audio" ) ;
71
+ const audioFolder = knownFolders . currentApp ( ) . getFolder ( ' audio' ) ;
78
72
console . log ( JSON . stringify ( audioFolder ) ) ;
79
73
80
74
let androidFormat ;
@@ -88,9 +82,7 @@ export class AudioDemo extends Observable {
88
82
androidEncoder = 3 ;
89
83
}
90
84
91
- const recordingPath = `${
92
- audioFolder . path
93
- } /recording.${ this . platformExtension ( ) } `;
85
+ const recordingPath = `${ audioFolder . path } /recording.${ this . platformExtension ( ) } ` ;
94
86
95
87
const recorderOptions : AudioRecorderOptions = {
96
88
filename : recordingPath ,
@@ -131,7 +123,7 @@ export class AudioDemo extends Observable {
131
123
} ) ;
132
124
133
125
this . isRecording = false ;
134
- alert ( " Recorder stopped." ) ;
126
+ alert ( ' Recorder stopped.' ) ;
135
127
this . _resetMeter ( ) ;
136
128
}
137
129
@@ -145,42 +137,38 @@ export class AudioDemo extends Observable {
145
137
146
138
private _resetMeter ( ) {
147
139
if ( this . _meterInterval ) {
148
- this . audioMeter = "0" ;
140
+ this . audioMeter = '0' ;
149
141
clearInterval ( this . _meterInterval ) ;
150
142
this . _meterInterval = undefined ;
151
143
}
152
144
}
153
145
154
146
public getFile ( args ) {
155
147
try {
156
- const audioFolder = knownFolders . currentApp ( ) . getFolder ( "audio" ) ;
157
- const recordedFile = audioFolder . getFile (
158
- `recording.${ this . platformExtension ( ) } `
159
- ) ;
148
+ const audioFolder = knownFolders . currentApp ( ) . getFolder ( 'audio' ) ;
149
+ const recordedFile = audioFolder . getFile ( `recording.${ this . platformExtension ( ) } ` ) ;
160
150
console . log ( JSON . stringify ( recordedFile ) ) ;
161
- console . log ( " recording exists: " + File . exists ( recordedFile . path ) ) ;
151
+ console . log ( ' recording exists: ' + File . exists ( recordedFile . path ) ) ;
162
152
this . recordedAudioFile = recordedFile . path ;
163
153
} catch ( ex ) {
164
154
console . log ( ex ) ;
165
155
}
166
156
}
167
157
168
158
public async playRecordedFile ( args ) {
169
- const audioFolder = knownFolders . currentApp ( ) . getFolder ( "audio" ) ;
170
- const recordedFile = audioFolder . getFile (
171
- `recording.${ this . platformExtension ( ) } `
172
- ) ;
173
- console . log ( "RECORDED FILE : " + JSON . stringify ( recordedFile ) ) ;
159
+ const audioFolder = knownFolders . currentApp ( ) . getFolder ( 'audio' ) ;
160
+ const recordedFile = audioFolder . getFile ( `recording.${ this . platformExtension ( ) } ` ) ;
161
+ console . log ( 'RECORDED FILE : ' + JSON . stringify ( recordedFile ) ) ;
174
162
175
163
const playerOptions : AudioPlayerOptions = {
176
164
audioFile : `~/audio/recording.${ this . platformExtension ( ) } ` ,
177
165
loop : false ,
178
166
completeCallback : async ( ) => {
179
- alert ( " Audio file complete." ) ;
167
+ alert ( ' Audio file complete.' ) ;
180
168
this . isPlaying = false ;
181
169
if ( ! playerOptions . loop ) {
182
170
await this . _player . dispose ( ) ;
183
- console . log ( " player disposed" ) ;
171
+ console . log ( ' player disposed' ) ;
184
172
}
185
173
} ,
186
174
@@ -191,12 +179,12 @@ export class AudioDemo extends Observable {
191
179
192
180
infoCallback : infoObject => {
193
181
console . log ( JSON . stringify ( infoObject ) ) ;
194
- dialogs . alert ( " Info callback" ) ;
182
+ dialogs . alert ( ' Info callback' ) ;
195
183
}
196
184
} ;
197
185
198
186
await this . _player . playFromFile ( playerOptions ) . catch ( err => {
199
- console . log ( " error playFromFile" ) ;
187
+ console . log ( ' error playFromFile' ) ;
200
188
this . isPlaying = false ;
201
189
} ) ;
202
190
@@ -211,32 +199,33 @@ export class AudioDemo extends Observable {
211
199
audioFile : filepath ,
212
200
loop : false ,
213
201
completeCallback : async ( ) => {
214
- alert ( " Audio file complete." ) ;
202
+ alert ( ' Audio file complete.' ) ;
215
203
await this . _player . dispose ( ) ;
216
204
this . isPlaying = false ;
217
- console . log ( " player disposed" ) ;
205
+ console . log ( ' player disposed' ) ;
218
206
} ,
219
207
errorCallback : errorObject => {
220
208
console . log ( JSON . stringify ( errorObject ) ) ;
221
209
this . isPlaying = false ;
222
210
} ,
223
211
infoCallback : args => {
224
- dialogs . alert ( " Info callback: " + args . info ) ;
212
+ dialogs . alert ( ' Info callback: ' + args . info ) ;
225
213
console . log ( JSON . stringify ( args ) ) ;
226
214
}
227
215
} ;
228
216
229
217
this . isPlaying = true ;
230
218
231
- if ( fileType === " localFile" ) {
219
+ if ( fileType === ' localFile' ) {
232
220
await this . _player . playFromFile ( playerOptions ) . catch ( ( ) => {
233
221
this . isPlaying = false ;
234
222
} ) ;
235
223
this . isPlaying = true ;
236
224
this . audioTrackDuration = await this . _player . getAudioTrackDuration ( ) ;
237
225
// start audio duration tracking
238
226
this . _startDurationTracking ( this . audioTrackDuration ) ;
239
- } else if ( fileType === "remoteFile" ) {
227
+ this . _startVolumeTracking ( ) ;
228
+ } else if ( fileType === 'remoteFile' ) {
240
229
await this . _player . playFromUrl ( playerOptions ) . catch ( ( ) => {
241
230
this . isPlaying = false ;
242
231
} ) ;
@@ -251,10 +240,10 @@ export class AudioDemo extends Observable {
251
240
* PLAY REMOTE AUDIO FILE
252
241
*/
253
242
public playRemoteFile ( args ) {
254
- console . log ( " playRemoteFile" ) ;
255
- const filepath = " http://www.noiseaddicts.com/samples_1w72b820/2514.mp3" ;
243
+ console . log ( ' playRemoteFile' ) ;
244
+ const filepath = ' http://www.noiseaddicts.com/samples_1w72b820/2514.mp3' ;
256
245
257
- this . playAudio ( filepath , " remoteFile" ) ;
246
+ this . playAudio ( filepath , ' remoteFile' ) ;
258
247
}
259
248
260
249
public resumePlayer ( ) {
@@ -266,8 +255,8 @@ export class AudioDemo extends Observable {
266
255
* PLAY LOCAL AUDIO FILE from app folder
267
256
*/
268
257
public playLocalFile ( args ) {
269
- let filepath = " ~/audio/angel.mp3" ;
270
- this . playAudio ( filepath , " localFile" ) ;
258
+ let filepath = ' ~/audio/angel.mp3' ;
259
+ this . playAudio ( filepath , ' localFile' ) ;
271
260
}
272
261
273
262
/**
@@ -285,14 +274,14 @@ export class AudioDemo extends Observable {
285
274
286
275
public async stopPlaying ( args ) {
287
276
await this . _player . dispose ( ) ;
288
- alert ( " Media Player Disposed." ) ;
277
+ alert ( ' Media Player Disposed.' ) ;
289
278
}
290
279
291
280
/**
292
281
* RESUME PLAYING
293
282
*/
294
283
public resumePlaying ( args ) {
295
- console . log ( " START" ) ;
284
+ console . log ( ' START' ) ;
296
285
this . _player . play ( ) ;
297
286
}
298
287
@@ -310,7 +299,7 @@ export class AudioDemo extends Observable {
310
299
311
300
private platformExtension ( ) {
312
301
// 'mp3'
313
- return `${ app . android ? " m4a" : " caf" } ` ;
302
+ return `${ app . android ? ' m4a' : ' caf' } ` ;
314
303
}
315
304
316
305
private async _startDurationTracking ( duration ) {
@@ -321,6 +310,15 @@ export class AudioDemo extends Observable {
321
310
} , 1000 ) ;
322
311
}
323
312
}
313
+
314
+ private _startVolumeTracking ( ) {
315
+ if ( this . _player ) {
316
+ const timerId = timer . setInterval ( ( ) => {
317
+ console . log ( 'volume tracking' , this . _player . volume ) ;
318
+ this . currentVolume = this . _player . volume ;
319
+ } , 2000 ) ;
320
+ }
321
+ }
324
322
}
325
323
326
324
export function ObservableProperty ( ) {
0 commit comments