1
1
import { check , request } from '@nativescript-community/perms' ;
2
2
import { Application } from '@nativescript/core' ;
3
- import { AudioRecorderOptions } from '..' ;
3
+ import { ANDROID_ENCODER_PCM , ANDROID_ENCODER_PCM_16 , AudioRecorderOptions } from '..' ;
4
4
5
5
export class TNSRecorder {
6
6
private _recorder : any ;
7
+ private _wavrecorder : any ;
7
8
8
- get android ( ) {
9
- return this . _recorder ;
10
- }
11
9
12
10
public static CAN_RECORD ( ) : boolean {
13
11
const pManager = Application . android . context . getPackageManager ( ) ;
@@ -32,57 +30,68 @@ export class TNSRecorder {
32
30
// bake the permission into this so the dev doesn't have to call it
33
31
await this . requestRecordPermission ( ) ;
34
32
33
+ const audioSource = options . source ? options . source : 0 ;
35
34
if ( this . _recorder ) {
36
35
// reset for reuse
37
36
this . _recorder . reset ( ) ;
38
37
} else {
39
- this . _recorder = new android . media . MediaRecorder ( ) ;
38
+ if ( options . encoder === ANDROID_ENCODER_PCM || options . encoder === ANDROID_ENCODER_PCM_16 ) {
39
+ console . log ( 'WaveRecorder' ) ;
40
+ //@ts -ignore
41
+ this . _wavrecorder = new com . github . squti . androidwaverecorder . WaveRecorder ( options . filename ) ;
42
+ } else {
43
+ this . _recorder = new android . media . MediaRecorder ( ) ;
44
+ }
40
45
}
41
-
42
- const audioSource = options . source ? options . source : 0 ;
43
- this . _recorder . setAudioSource ( audioSource ) ;
44
-
45
- const outFormat = options . format ? options . format : 0 ;
46
- this . _recorder . setOutputFormat ( outFormat ) ;
47
-
48
- const encoder = options . encoder ? options . encoder : 0 ;
49
- this . _recorder . setAudioEncoder ( encoder ) ;
50
-
51
- if ( options . channels ) {
52
- this . _recorder . setAudioChannels ( options . channels ) ;
53
- }
54
- if ( options . sampleRate ) {
55
- this . _recorder . setAudioSamplingRate ( options . sampleRate ) ;
56
- }
57
- if ( options . bitRate ) {
58
- this . _recorder . setAudioEncodingBitRate ( options . bitRate ) ;
59
- }
60
- if ( options . maxDuration ) {
61
- this . _recorder . setMaxDuration ( options . maxDuration ) ;
46
+ if ( this . _recorder ) {
47
+ this . _recorder . setAudioSource ( audioSource ) ;
48
+ const outFormat = options . format ? options . format : 0 ;
49
+ this . _recorder . setOutputFormat ( outFormat ) ;
50
+
51
+ const encoder = options . encoder ? options . encoder : 0 ;
52
+ this . _recorder . setAudioEncoder ( encoder ) ;
53
+
54
+ if ( options . channels ) {
55
+ this . _recorder . setAudioChannels ( options . channels ) ;
56
+ }
57
+ if ( options . sampleRate ) {
58
+ this . _recorder . setAudioSamplingRate ( options . sampleRate ) ;
59
+ }
60
+ if ( options . bitRate ) {
61
+ this . _recorder . setAudioEncodingBitRate ( options . bitRate ) ;
62
+ }
63
+ if ( options . maxDuration ) {
64
+ this . _recorder . setMaxDuration ( options . maxDuration ) ;
65
+ }
66
+ // On Error
67
+ options . errorCallback &&
68
+ this . _recorder . setOnErrorListener (
69
+ new android . media . MediaRecorder . OnErrorListener ( {
70
+ onError : ( recorder : any , error : number , extra : number ) => {
71
+ options . errorCallback ( { recorder, error, extra } ) ;
72
+ }
73
+ } )
74
+ ) ;
75
+
76
+ // On Info
77
+ options . infoCallback &&
78
+ this . _recorder . setOnInfoListener (
79
+ new android . media . MediaRecorder . OnInfoListener ( {
80
+ onInfo : ( recorder : any , info : number , extra : number ) => {
81
+ options . infoCallback ( { recorder, info, extra } ) ;
82
+ }
83
+ } )
84
+ ) ;
85
+ this . _recorder . setOutputFile ( options . filename ) ;
86
+ this . _recorder . prepare ( ) ;
87
+ this . _recorder . start ( ) ;
88
+ } else if ( this . _wavrecorder ) {
89
+ this . _wavrecorder . waveConfig . sampleRate = options . sampleRate || 44100 ;
90
+ this . _wavrecorder . waveConfig . channels = options . channels === 1 ? android . media . AudioFormat . CHANNEL_IN_MONO : 2 ;
91
+ this . _wavrecorder . waveConfig . audioEncoding = options . encoder === ANDROID_ENCODER_PCM_16 ? android . media . AudioFormat . ENCODING_PCM_16BIT : android . media . AudioFormat . ENCODING_PCM_8BIT ;
92
+ this . _wavrecorder . startRecording ( ) ;
62
93
}
63
94
64
- this . _recorder . setOutputFile ( options . filename ) ;
65
-
66
- // On Error
67
- this . _recorder . setOnErrorListener (
68
- new android . media . MediaRecorder . OnErrorListener ( {
69
- onError : ( recorder : any , error : number , extra : number ) => {
70
- options . errorCallback ( { recorder, error, extra } ) ;
71
- }
72
- } )
73
- ) ;
74
-
75
- // On Info
76
- this . _recorder . setOnInfoListener (
77
- new android . media . MediaRecorder . OnInfoListener ( {
78
- onInfo : ( recorder : any , info : number , extra : number ) => {
79
- options . infoCallback ( { recorder, info, extra } ) ;
80
- }
81
- } )
82
- ) ;
83
-
84
- this . _recorder . prepare ( ) ;
85
- this . _recorder . start ( ) ;
86
95
}
87
96
88
97
public getMeters ( ) : number {
@@ -92,19 +101,27 @@ export class TNSRecorder {
92
101
93
102
public async pause ( ) {
94
103
if ( this . _recorder ) {
104
+ // not working yet with pcm
95
105
this . _recorder . pause ( ) ;
106
+ } else if ( this . _wavrecorder ) {
107
+ this . _wavrecorder . pauseRecording ( ) ;
96
108
}
97
109
}
98
110
99
111
public async resume ( ) {
100
112
if ( this . _recorder ) {
113
+ // not working yet with pcm
101
114
this . _recorder . resume ( ) ;
115
+ } else if ( this . _wavrecorder ) {
116
+ this . _wavrecorder . resumeRecording ( ) ;
102
117
}
103
118
}
104
119
105
120
public async stop ( ) {
106
121
if ( this . _recorder ) {
107
122
this . _recorder . stop ( ) ;
123
+ } else if ( this . _wavrecorder ) {
124
+ this . _wavrecorder . stopRecording ( ) ;
108
125
}
109
126
}
110
127
0 commit comments