community-cordova-plugin-dbmeter 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +8 -0
- package/.eslintrc +15 -0
- package/.github/FUNDING.yml +3 -0
- package/.travis.yml +4 -0
- package/LICENSE +201 -0
- package/README.md +109 -0
- package/dbmeter.js +68 -0
- package/package.json +33 -0
- package/plugin.xml +37 -0
- package/src/android/DBMeter.java +200 -0
- package/src/android/PermissionHelper.java +138 -0
- package/src/ios/DBMeter.swift +158 -0
- package/tests/package.json +4 -0
- package/tests/plugin.xml +11 -0
- package/tests/tests.js +212 -0
package/.editorconfig
ADDED
package/.eslintrc
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"rules": {
|
|
3
|
+
"array-bracket-spacing": [ 2, "always" ],
|
|
4
|
+
"comma-dangle": [ 2, "never" ],
|
|
5
|
+
"eol-last": 2,
|
|
6
|
+
"indent": [ 2, 2, { "SwitchCase": 1 } ],
|
|
7
|
+
"no-multiple-empty-lines": 2,
|
|
8
|
+
"no-unused-vars": 2,
|
|
9
|
+
"object-curly-spacing": [ 2, "always" ],
|
|
10
|
+
"quotes": [ 2, "single", "avoid-escape" ],
|
|
11
|
+
"semi": 2,
|
|
12
|
+
"strict": 0,
|
|
13
|
+
"space-before-blocks": [ 2, "always" ]
|
|
14
|
+
}
|
|
15
|
+
}
|
package/.travis.yml
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/community-cordova-plugin-dbmeter)
|
|
2
|
+
[](https://www.npmjs.com/package/community-cordova-plugin-dbmeter)
|
|
3
|
+
|
|
4
|
+
#### This is a fork of the original plugin cordova-plugin-dbmeter
|
|
5
|
+
|
|
6
|
+
# community-cordova-plugin-dbmeter
|
|
7
|
+
|
|
8
|
+
This plugin defines a global DBMeter object, which permits to get the decibel values from the microphone.
|
|
9
|
+
Although the object is in the global scope, it is not available until after the deviceready event.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
document.addEventListener("deviceready", onDeviceReady, false);
|
|
13
|
+
function onDeviceReady() {
|
|
14
|
+
DBMeter.start(function(dB){
|
|
15
|
+
console.log(dB);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
cordova plugin add cordova-plugin-add-swift-support
|
|
24
|
+
cordova plugin add community-cordova-plugin-dbmeter
|
|
25
|
+
```
|
|
26
|
+
The iOS part is written in Swift so the [Swift support plugin](https://github.com/akofman/cordova-plugin-add-swift-support) is needed.
|
|
27
|
+
|
|
28
|
+
## Supported Platforms
|
|
29
|
+
|
|
30
|
+
- iOS
|
|
31
|
+
- Android
|
|
32
|
+
|
|
33
|
+
## iOS Quirks
|
|
34
|
+
|
|
35
|
+
Since iOS 10 it's mandatory to provide an usage description in the info.plist if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will displayed as part of the permission dialog box, but if you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.
|
|
36
|
+
|
|
37
|
+
This plugins requires the following usage description:
|
|
38
|
+
|
|
39
|
+
- NSMicrophoneUsageDescription describes the reason the app accesses the user's microphone.
|
|
40
|
+
|
|
41
|
+
To add this entry into the info.plist, you can use the `edit-config` tag in the platform section of your `config.xml` like this:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
<edit-config target="NSMicrophoneUsageDescription" file="*-Info.plist" mode="merge">
|
|
45
|
+
<string>need microphone access to record sounds</string>
|
|
46
|
+
</edit-config>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Methods
|
|
50
|
+
|
|
51
|
+
## `DBMeter.start(success, error)`
|
|
52
|
+
start listening.
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
DBMeter.start(function(dB){
|
|
56
|
+
console.log(dB);
|
|
57
|
+
}, function(e){
|
|
58
|
+
console.log('code: ' + e.code + ', message: ' + e.message);
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## `DBMeter.stop(success, error)`
|
|
63
|
+
stop listening.
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
DBMeter.stop(function(){
|
|
67
|
+
console.log("DBMeter well stopped");
|
|
68
|
+
}, function(e){
|
|
69
|
+
console.log('code: ' + e.code + ', message: ' + e.message);
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
:warning: If the DBMeter is stopped while is not listening, an error will be
|
|
74
|
+
triggered and can be handle from the second callback argument.
|
|
75
|
+
|
|
76
|
+
## `DBMeter.isListening(success, error)`
|
|
77
|
+
retrieve from the success callback whether
|
|
78
|
+
the DBMeter is listening.
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
DBMeter.isListening(function(isListening){
|
|
82
|
+
console.log(isListening);
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## `DBMeter.delete(success, error)`
|
|
87
|
+
delete the DBMeter instance.
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
DBMeter.delete(function(){
|
|
91
|
+
console.log("Well done !");
|
|
92
|
+
}, function(e){
|
|
93
|
+
console.log('code: ' + e.code + ', message: ' + e.message);
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
:warning: If the DBMeter has not been started once before deleting, an error will be
|
|
97
|
+
triggered and can be handle from the second callback argument.
|
|
98
|
+
|
|
99
|
+
## App
|
|
100
|
+
|
|
101
|
+
An app is available in the app folder and is generated from the [Cordova Plugin Test Framework](https://github.com/apache/cordova-plugin-test-framework).
|
|
102
|
+
It permits to launch auto tests and manual tests.
|
|
103
|
+
|
|
104
|
+
To install it, please follow these steps :
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
cd app && cordova platform add android|ios
|
|
108
|
+
cordova run android|ios --device
|
|
109
|
+
```
|
package/dbmeter.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var DBMeter = {
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Delete the plugin.
|
|
5
|
+
* Remove its native instance from the memory.
|
|
6
|
+
*
|
|
7
|
+
* @param {success} callback in case of success
|
|
8
|
+
* @param {error} callback in case of error
|
|
9
|
+
*/
|
|
10
|
+
delete: function(success, error) {
|
|
11
|
+
cordova.exec(
|
|
12
|
+
success,
|
|
13
|
+
error,
|
|
14
|
+
'DBMeter', 'destroy', []
|
|
15
|
+
);
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Starts listening the audio signal.
|
|
20
|
+
*
|
|
21
|
+
* Returns the current decibel value from the success callback parameter
|
|
22
|
+
* as a float value.
|
|
23
|
+
* @param {success} callback in case of success
|
|
24
|
+
* @param {error} callback in case of error
|
|
25
|
+
*/
|
|
26
|
+
start: function(success, error) {
|
|
27
|
+
cordova.exec(
|
|
28
|
+
success,
|
|
29
|
+
error,
|
|
30
|
+
'DBMeter', 'start', []
|
|
31
|
+
);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Stops listening any audio signal.
|
|
36
|
+
*
|
|
37
|
+
* @param {success} callback in case of success
|
|
38
|
+
* @param {error} callback in case of error
|
|
39
|
+
*/
|
|
40
|
+
stop: function(success, error) {
|
|
41
|
+
cordova.exec(
|
|
42
|
+
success,
|
|
43
|
+
error,
|
|
44
|
+
'DBMeter', 'stop', []
|
|
45
|
+
);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns whether the DBMeter is listening.
|
|
50
|
+
*
|
|
51
|
+
* @param {success} callback in case of success
|
|
52
|
+
* @param {error} callback in case of error
|
|
53
|
+
*/
|
|
54
|
+
isListening: function(success, error) {
|
|
55
|
+
cordova.exec(
|
|
56
|
+
success,
|
|
57
|
+
error,
|
|
58
|
+
'DBMeter', 'isListening', []
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
DBMeter.ERROR_CODES = {
|
|
64
|
+
'0':'DBMETER_NOT_INITIALIZED',
|
|
65
|
+
'1':'DBMETER_NOT_LISTENING'
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
module.exports = DBMeter;
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "community-cordova-plugin-dbmeter",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Cordova DBMeter plugin",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Alexis Kofman"
|
|
7
|
+
},
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"cordova": {
|
|
10
|
+
"id": "community-cordova-plugin-dbmeter",
|
|
11
|
+
"platforms": [
|
|
12
|
+
"ios",
|
|
13
|
+
"android"
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"repository": "EYALIN/community-cordova-plugin-dbmeter/tree/master",
|
|
17
|
+
"homepage": "https://github.com/EYALIN/community-cordova-plugin-dbmeter/tree/master",
|
|
18
|
+
"funding": "https://github.com/sponsors/EYALIN",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"cordova",
|
|
21
|
+
"ios",
|
|
22
|
+
"android",
|
|
23
|
+
"dbmeter",
|
|
24
|
+
"audio",
|
|
25
|
+
"volume",
|
|
26
|
+
"sound",
|
|
27
|
+
"decibel",
|
|
28
|
+
"ecosystem:cordova",
|
|
29
|
+
"cordova-ios",
|
|
30
|
+
"cordova-android",
|
|
31
|
+
"cordova:plugin"
|
|
32
|
+
]
|
|
33
|
+
}
|
package/plugin.xml
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="community-cordova-plugin-dbmeter" version="2.2.0">
|
|
3
|
+
|
|
4
|
+
<name>DBMeter</name>
|
|
5
|
+
<license>Apache 2.0</license>
|
|
6
|
+
<description>Cordova DBMeter plugin</description>
|
|
7
|
+
<keywords>cordova,phonegap,volume,sound,audio,decibel</keywords>
|
|
8
|
+
|
|
9
|
+
<platform name="android">
|
|
10
|
+
<config-file target="AndroidManifest.xml" parent="/*">
|
|
11
|
+
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
12
|
+
</config-file>
|
|
13
|
+
|
|
14
|
+
<config-file target="res/xml/config.xml" parent="/*">
|
|
15
|
+
<feature name="DBMeter">
|
|
16
|
+
<param name="android-package" value="org.apache.cordova.dbmeter.DBMeter"/>
|
|
17
|
+
</feature>
|
|
18
|
+
</config-file>
|
|
19
|
+
|
|
20
|
+
<source-file src="src/android/DBMeter.java" target-dir="src/org/apache/cordova/dbmeter" />
|
|
21
|
+
<source-file src="src/android/PermissionHelper.java" target-dir="src/org/apache/cordova/dbmeter" />
|
|
22
|
+
</platform>
|
|
23
|
+
|
|
24
|
+
<platform name="ios">
|
|
25
|
+
<config-file target="config.xml" parent="/*">
|
|
26
|
+
<feature name="DBMeter">
|
|
27
|
+
<param name="ios-package" value="DBMeter" />
|
|
28
|
+
</feature>
|
|
29
|
+
</config-file>
|
|
30
|
+
|
|
31
|
+
<source-file src="src/ios/DBMeter.swift" />
|
|
32
|
+
</platform>
|
|
33
|
+
|
|
34
|
+
<js-module src="dbmeter.js" name="DBMeter">
|
|
35
|
+
<clobbers target="DBMeter" />
|
|
36
|
+
</js-module>
|
|
37
|
+
</plugin>
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
package org.apache.cordova.dbmeter;
|
|
2
|
+
|
|
3
|
+
import android.Manifest;
|
|
4
|
+
import android.media.AudioFormat;
|
|
5
|
+
import android.media.AudioManager;
|
|
6
|
+
import android.media.AudioRecord;
|
|
7
|
+
import android.media.AudioTrack;
|
|
8
|
+
import android.media.MediaRecorder;
|
|
9
|
+
|
|
10
|
+
import org.apache.cordova.CallbackContext;
|
|
11
|
+
import org.apache.cordova.CordovaPlugin;
|
|
12
|
+
import org.apache.cordova.LOG;
|
|
13
|
+
import org.apache.cordova.PluginResult;
|
|
14
|
+
|
|
15
|
+
import org.json.JSONArray;
|
|
16
|
+
import org.json.JSONException;
|
|
17
|
+
import org.json.JSONObject;
|
|
18
|
+
|
|
19
|
+
import java.util.Timer;
|
|
20
|
+
import java.util.TimerTask;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* This plugin provides the decibel level from the microphone.
|
|
24
|
+
*/
|
|
25
|
+
public class DBMeter extends CordovaPlugin {
|
|
26
|
+
|
|
27
|
+
private static final String LOG_TAG = "DBMeter";
|
|
28
|
+
private static final int REQ_CODE = 0;
|
|
29
|
+
private AudioRecord audioRecord;
|
|
30
|
+
private short[] buffer;
|
|
31
|
+
private Timer timer;
|
|
32
|
+
private boolean isListening = false;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@Override
|
|
36
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
37
|
+
//LOG.setLogLevel(LOG.DEBUG);
|
|
38
|
+
|
|
39
|
+
if (!PermissionHelper.hasPermission(this, Manifest.permission.RECORD_AUDIO)) {
|
|
40
|
+
PermissionHelper.requestPermission(this, REQ_CODE, Manifest.permission.RECORD_AUDIO);
|
|
41
|
+
} else {
|
|
42
|
+
if (action.equals("start")) {
|
|
43
|
+
this.start(callbackContext);
|
|
44
|
+
} else if (action.equals("stop")) {
|
|
45
|
+
this.stop(callbackContext);
|
|
46
|
+
} else if (action.equals("isListening")) {
|
|
47
|
+
this.isListening(callbackContext);
|
|
48
|
+
} else if (action.equals("destroy")) {
|
|
49
|
+
this.destroy(callbackContext);
|
|
50
|
+
} else {
|
|
51
|
+
LOG.e(LOG_TAG, "Not a valid action: " + action);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Permits to free the memory from the audioRecord instance
|
|
60
|
+
*
|
|
61
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
62
|
+
*/
|
|
63
|
+
public void destroy(CallbackContext callbackContext) {
|
|
64
|
+
if (this.audioRecord != null) {
|
|
65
|
+
this.isListening = false;
|
|
66
|
+
this.audioRecord.stop();
|
|
67
|
+
this.audioRecord = null;
|
|
68
|
+
if (this.timer != null) {
|
|
69
|
+
this.timer.cancel();
|
|
70
|
+
this.timer = null;
|
|
71
|
+
}
|
|
72
|
+
callbackContext.success();
|
|
73
|
+
} else {
|
|
74
|
+
sendPluginError(callbackContext, PluginError.DBMETER_NOT_INITIALIZED, "DBMeter is not initialized");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Starts listening the audio signal and sends dB values as a
|
|
80
|
+
* {@link org.apache.cordova.PluginResult PluginResult} using a
|
|
81
|
+
* {@link java.util.TimerTask TimerTask}.
|
|
82
|
+
*
|
|
83
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
84
|
+
*/
|
|
85
|
+
public void start(final CallbackContext callbackContext) {
|
|
86
|
+
final DBMeter that = this;
|
|
87
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
88
|
+
public void run() {
|
|
89
|
+
if (that.audioRecord == null) {
|
|
90
|
+
that.isListening = false;
|
|
91
|
+
int rate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
|
|
92
|
+
int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
|
|
93
|
+
|
|
94
|
+
that.audioRecord = new AudioRecord(
|
|
95
|
+
MediaRecorder.AudioSource.MIC,
|
|
96
|
+
rate,
|
|
97
|
+
AudioFormat.CHANNEL_IN_MONO,
|
|
98
|
+
AudioFormat.ENCODING_PCM_16BIT,
|
|
99
|
+
bufferSize);
|
|
100
|
+
|
|
101
|
+
that.buffer = new short[bufferSize];
|
|
102
|
+
}
|
|
103
|
+
if (!that.isListening) {
|
|
104
|
+
that.isListening = true;
|
|
105
|
+
that.audioRecord.startRecording();
|
|
106
|
+
|
|
107
|
+
that.timer = new Timer(LOG_TAG, true);
|
|
108
|
+
|
|
109
|
+
//start calling run in a timertask
|
|
110
|
+
TimerTask timerTask = new TimerTask() {
|
|
111
|
+
public void run() {
|
|
112
|
+
int readSize = that.audioRecord.read(that.buffer, 0, that.buffer.length);
|
|
113
|
+
double db = 0;
|
|
114
|
+
double maxAmplitude = 0;
|
|
115
|
+
for (int i = 0; i < readSize; i++) {
|
|
116
|
+
if (Math.abs(that.buffer[i]) > maxAmplitude) {
|
|
117
|
+
maxAmplitude = Math.abs(that.buffer[i]);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (maxAmplitude != 0) {
|
|
122
|
+
db = 20.0 * Math.log10(maxAmplitude / 32767.0) + 90;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
LOG.d(LOG_TAG, Double.toString(db));
|
|
126
|
+
|
|
127
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, (float) db);
|
|
128
|
+
result.setKeepCallback(true);
|
|
129
|
+
callbackContext.sendPluginResult(result);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
that.timer.scheduleAtFixedRate(timerTask, 0, 100);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Stops listening the audio signal.
|
|
140
|
+
* Even if stopped, the {@link android.media.AudioRecord AudioRecord} instance still exist.
|
|
141
|
+
* To destroy this instance, please use the {@link #destroy(CallbackContext) destroy} method.
|
|
142
|
+
*
|
|
143
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
144
|
+
*/
|
|
145
|
+
public void stop(final CallbackContext callbackContext) {
|
|
146
|
+
final DBMeter that = this;
|
|
147
|
+
cordova.getThreadPool().execute(new Runnable() {
|
|
148
|
+
public void run() {
|
|
149
|
+
if (that.isListening) {
|
|
150
|
+
that.isListening = false;
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
if (that.timer != null) {
|
|
154
|
+
that.timer.cancel();
|
|
155
|
+
}
|
|
156
|
+
if (that.audioRecord != null) {
|
|
157
|
+
that.audioRecord.stop();
|
|
158
|
+
}
|
|
159
|
+
callbackContext.success();
|
|
160
|
+
|
|
161
|
+
} else {
|
|
162
|
+
sendPluginError(callbackContext, PluginError.DBMETER_NOT_LISTENING, "DBMeter is not listening");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns whether the DBMeter is listening.
|
|
170
|
+
*
|
|
171
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
172
|
+
*/
|
|
173
|
+
public void isListening(final CallbackContext callbackContext) {
|
|
174
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, this.isListening);
|
|
175
|
+
callbackContext.sendPluginResult(result);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Convenient method to send plugin errors.
|
|
180
|
+
*
|
|
181
|
+
* @param callbackContext The callback context used when calling back into JavaScript.
|
|
182
|
+
* @param error The error code to return
|
|
183
|
+
* @param message The error message to return
|
|
184
|
+
*/
|
|
185
|
+
private void sendPluginError(CallbackContext callbackContext, PluginError error, String message) {
|
|
186
|
+
JSONObject jsonObject = new JSONObject();
|
|
187
|
+
try {
|
|
188
|
+
jsonObject.put("code", error.ordinal());
|
|
189
|
+
jsonObject.put("message", message);
|
|
190
|
+
} catch (JSONException e) {
|
|
191
|
+
e.printStackTrace();
|
|
192
|
+
}
|
|
193
|
+
callbackContext.error(jsonObject);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public enum PluginError {
|
|
197
|
+
DBMETER_NOT_INITIALIZED,
|
|
198
|
+
DBMETER_NOT_LISTENING
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
or more contributor license agreements. See the NOTICE file
|
|
4
|
+
distributed with this work for additional information
|
|
5
|
+
regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
to you under the Apache License, Version 2.0 (the
|
|
7
|
+
"License"); you may not use this file except in compliance
|
|
8
|
+
with the License. You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing,
|
|
13
|
+
software distributed under the License is distributed on an
|
|
14
|
+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
KIND, either express or implied. See the License for the
|
|
16
|
+
specific language governing permissions and limitations
|
|
17
|
+
under the License.
|
|
18
|
+
*/
|
|
19
|
+
package org.apache.cordova.dbmeter;
|
|
20
|
+
|
|
21
|
+
import java.lang.reflect.InvocationTargetException;
|
|
22
|
+
import java.lang.reflect.Method;
|
|
23
|
+
import java.util.Arrays;
|
|
24
|
+
|
|
25
|
+
import org.apache.cordova.CordovaInterface;
|
|
26
|
+
import org.apache.cordova.CordovaPlugin;
|
|
27
|
+
import org.apache.cordova.LOG;
|
|
28
|
+
|
|
29
|
+
import android.content.pm.PackageManager;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This class provides reflective methods for permission requesting and checking so that plugins
|
|
33
|
+
* written for cordova-android 5.0.0+ can still compile with earlier cordova-android versions.
|
|
34
|
+
*/
|
|
35
|
+
public class PermissionHelper {
|
|
36
|
+
private static final String LOG_TAG = "CordovaPermissionHelper";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Requests a "dangerous" permission for the application at runtime. This is a helper method
|
|
40
|
+
* alternative to cordovaInterface.requestPermission() that does not require the project to be
|
|
41
|
+
* built with cordova-android 5.0.0+
|
|
42
|
+
*
|
|
43
|
+
* @param plugin The plugin the permission is being requested for
|
|
44
|
+
* @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
|
|
45
|
+
* along with the result of the permission request
|
|
46
|
+
* @param permission The permission to be requested
|
|
47
|
+
*/
|
|
48
|
+
public static void requestPermission(CordovaPlugin plugin, int requestCode, String permission) {
|
|
49
|
+
PermissionHelper.requestPermissions(plugin, requestCode, new String[] {permission});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Requests "dangerous" permissions for the application at runtime. This is a helper method
|
|
54
|
+
* alternative to cordovaInterface.requestPermissions() that does not require the project to be
|
|
55
|
+
* built with cordova-android 5.0.0+
|
|
56
|
+
*
|
|
57
|
+
* @param plugin The plugin the permissions are being requested for
|
|
58
|
+
* @param requestCode A requestCode to be passed to the plugin's onRequestPermissionResult()
|
|
59
|
+
* along with the result of the permissions request
|
|
60
|
+
* @param permissions The permissions to be requested
|
|
61
|
+
*/
|
|
62
|
+
public static void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
|
|
63
|
+
try {
|
|
64
|
+
Method requestPermission = CordovaInterface.class.getDeclaredMethod(
|
|
65
|
+
"requestPermissions", CordovaPlugin.class, int.class, String[].class);
|
|
66
|
+
|
|
67
|
+
// If there is no exception, then this is cordova-android 5.0.0+
|
|
68
|
+
requestPermission.invoke(plugin.cordova, plugin, requestCode, permissions);
|
|
69
|
+
} catch (NoSuchMethodException noSuchMethodException) {
|
|
70
|
+
// cordova-android version is less than 5.0.0, so permission is implicitly granted
|
|
71
|
+
LOG.d(LOG_TAG, "No need to request permissions " + Arrays.toString(permissions));
|
|
72
|
+
|
|
73
|
+
// Notify the plugin that all were granted by using more reflection
|
|
74
|
+
deliverPermissionResult(plugin, requestCode, permissions);
|
|
75
|
+
} catch (IllegalAccessException illegalAccessException) {
|
|
76
|
+
// Should never be caught; this is a public method
|
|
77
|
+
LOG.e(LOG_TAG, "IllegalAccessException when requesting permissions " + Arrays.toString(permissions), illegalAccessException);
|
|
78
|
+
} catch(InvocationTargetException invocationTargetException) {
|
|
79
|
+
// This method does not throw any exceptions, so this should never be caught
|
|
80
|
+
LOG.e(LOG_TAG, "invocationTargetException when requesting permissions " + Arrays.toString(permissions), invocationTargetException);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Checks at runtime to see if the application has been granted a permission. This is a helper
|
|
86
|
+
* method alternative to cordovaInterface.hasPermission() that does not require the project to
|
|
87
|
+
* be built with cordova-android 5.0.0+
|
|
88
|
+
*
|
|
89
|
+
* @param plugin The plugin the permission is being checked against
|
|
90
|
+
* @param permission The permission to be checked
|
|
91
|
+
*
|
|
92
|
+
* @return True if the permission has already been granted and false otherwise
|
|
93
|
+
*/
|
|
94
|
+
public static boolean hasPermission(CordovaPlugin plugin, String permission) {
|
|
95
|
+
try {
|
|
96
|
+
Method hasPermission = CordovaInterface.class.getDeclaredMethod("hasPermission", String.class);
|
|
97
|
+
|
|
98
|
+
// If there is no exception, then this is cordova-android 5.0.0+
|
|
99
|
+
return (Boolean) hasPermission.invoke(plugin.cordova, permission);
|
|
100
|
+
} catch (NoSuchMethodException noSuchMethodException) {
|
|
101
|
+
// cordova-android version is less than 5.0.0, so permission is implicitly granted
|
|
102
|
+
LOG.d(LOG_TAG, "No need to check for permission " + permission);
|
|
103
|
+
return true;
|
|
104
|
+
} catch (IllegalAccessException illegalAccessException) {
|
|
105
|
+
// Should never be caught; this is a public method
|
|
106
|
+
LOG.e(LOG_TAG, "IllegalAccessException when checking permission " + permission, illegalAccessException);
|
|
107
|
+
} catch(InvocationTargetException invocationTargetException) {
|
|
108
|
+
// This method does not throw any exceptions, so this should never be caught
|
|
109
|
+
LOG.e(LOG_TAG, "invocationTargetException when checking permission " + permission, invocationTargetException);
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private static void deliverPermissionResult(CordovaPlugin plugin, int requestCode, String[] permissions) {
|
|
115
|
+
// Generate the request results
|
|
116
|
+
int[] requestResults = new int[permissions.length];
|
|
117
|
+
Arrays.fill(requestResults, PackageManager.PERMISSION_GRANTED);
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
Method onRequestPermissionResult = CordovaPlugin.class.getDeclaredMethod(
|
|
121
|
+
"onRequestPermissionResult", int.class, String[].class, int[].class);
|
|
122
|
+
|
|
123
|
+
onRequestPermissionResult.invoke(plugin, requestCode, permissions, requestResults);
|
|
124
|
+
} catch (NoSuchMethodException noSuchMethodException) {
|
|
125
|
+
// Should never be caught since the plugin must be written for cordova-android 5.0.0+ if it
|
|
126
|
+
// made it to this point
|
|
127
|
+
LOG.e(LOG_TAG, "NoSuchMethodException when delivering permissions results", noSuchMethodException);
|
|
128
|
+
} catch (IllegalAccessException illegalAccessException) {
|
|
129
|
+
// Should never be caught; this is a public method
|
|
130
|
+
LOG.e(LOG_TAG, "IllegalAccessException when delivering permissions results", illegalAccessException);
|
|
131
|
+
} catch(InvocationTargetException invocationTargetException) {
|
|
132
|
+
// This method may throw a JSONException. We are just duplicating cordova-android's
|
|
133
|
+
// exception handling behavior here; all it does is log the exception in CordovaActivity,
|
|
134
|
+
// print the stacktrace, and ignore it
|
|
135
|
+
LOG.e(LOG_TAG, "InvocationTargetException when delivering permissions results", invocationTargetException);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import AVFoundation
|
|
3
|
+
|
|
4
|
+
@objc(DBMeter) class DBMeter : CDVPlugin {
|
|
5
|
+
|
|
6
|
+
private let LOG_TAG = "DBMeter"
|
|
7
|
+
private let REQ_CODE = 0
|
|
8
|
+
private var isListening: Bool = false
|
|
9
|
+
private var audioRecorder: AVAudioRecorder!
|
|
10
|
+
private var command: CDVInvokedUrlCommand!
|
|
11
|
+
private var timer: DispatchSourceTimer!
|
|
12
|
+
private var isTimerExists: Bool = false
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
This plugin provides the decibel level from the microphone.
|
|
16
|
+
*/
|
|
17
|
+
init(commandDelegate: CDVCommandDelegate) {
|
|
18
|
+
super.init()
|
|
19
|
+
self.commandDelegate = commandDelegate
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
override init() {
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
Permits to free the memory from the audioRecord instance
|
|
27
|
+
*/
|
|
28
|
+
@objc(destroy:)
|
|
29
|
+
func destroy(command: CDVInvokedUrlCommand) {
|
|
30
|
+
if (self.isListening) {
|
|
31
|
+
self.timer.suspend()
|
|
32
|
+
self.isListening = false
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
self.command = nil
|
|
36
|
+
|
|
37
|
+
if (self.audioRecorder != nil) {
|
|
38
|
+
|
|
39
|
+
self.audioRecorder.stop()
|
|
40
|
+
self.audioRecorder = nil
|
|
41
|
+
|
|
42
|
+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
|
|
43
|
+
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
|
|
44
|
+
} else {
|
|
45
|
+
self.sendPluginError(callbackId: command.callbackId, errorCode: PluginError.DBMETER_NOT_INITIALIZED, errorMessage: "DBMeter is not initialized")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
Starts listening the audio signal and sends dB values as a
|
|
51
|
+
CDVPluginResult keeping the same calback alive.
|
|
52
|
+
*/
|
|
53
|
+
@objc(start:)
|
|
54
|
+
func start(command: CDVInvokedUrlCommand) {
|
|
55
|
+
self.commandDelegate!.run(inBackground:{
|
|
56
|
+
self.command = command
|
|
57
|
+
|
|
58
|
+
if (!self.isTimerExists) {
|
|
59
|
+
self.timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
|
|
60
|
+
self.timer.scheduleRepeating(deadline: .now(), interval: DispatchTimeInterval.milliseconds(300))
|
|
61
|
+
self.timer.setEventHandler(handler: self.timerCallBack)
|
|
62
|
+
self.isTimerExists = true
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (self.audioRecorder == nil) {
|
|
66
|
+
let url: URL = URL.init(fileURLWithPath: "/dev/null")
|
|
67
|
+
|
|
68
|
+
let settings : [String:Any] = [
|
|
69
|
+
AVFormatIDKey: Int(kAudioFormatAppleLossless),
|
|
70
|
+
AVSampleRateKey: 44100.0,
|
|
71
|
+
AVNumberOfChannelsKey: 1 as NSNumber,
|
|
72
|
+
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
do {
|
|
76
|
+
let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
|
|
77
|
+
try audioSession.setCategory(AVAudioSession.Category.playAndRecord)
|
|
78
|
+
try audioSession.setActive(true)
|
|
79
|
+
|
|
80
|
+
self.audioRecorder = try AVAudioRecorder(url: url, settings: settings)
|
|
81
|
+
self.audioRecorder.isMeteringEnabled = true
|
|
82
|
+
} catch {
|
|
83
|
+
self.sendPluginError(callbackId: command.callbackId, errorCode: PluginError.DBMETER_NOT_INITIALIZED, errorMessage: "Error while initializing DBMeter")
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!self.isListening) {
|
|
87
|
+
self.isListening = true
|
|
88
|
+
self.audioRecorder.record()
|
|
89
|
+
|
|
90
|
+
self.timer.resume()
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
Stops listening the audio signal.
|
|
97
|
+
Even if stopped, the AVAudioRecorder instance still exist.
|
|
98
|
+
To destroy this instance, please use the destroy method.
|
|
99
|
+
*/
|
|
100
|
+
@objc(stop:)
|
|
101
|
+
public func stop(command: CDVInvokedUrlCommand) {
|
|
102
|
+
self.commandDelegate!.run(inBackground: {
|
|
103
|
+
if (self.isListening) {
|
|
104
|
+
self.isListening = false
|
|
105
|
+
|
|
106
|
+
if (self.audioRecorder != nil && self.audioRecorder.isRecording) {
|
|
107
|
+
self.timer.suspend()
|
|
108
|
+
self.audioRecorder.stop()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
|
|
112
|
+
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
|
|
113
|
+
} else {
|
|
114
|
+
self.sendPluginError(callbackId: command.callbackId, errorCode: PluginError.DBMETER_NOT_LISTENING, errorMessage: "DBMeter is not listening")
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
Returns whether the DBMeter is listening.
|
|
121
|
+
*/
|
|
122
|
+
@objc(isListening:)
|
|
123
|
+
func isListening(command: CDVInvokedUrlCommand?) -> Bool {
|
|
124
|
+
if (command != nil) {
|
|
125
|
+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: self.isListening)
|
|
126
|
+
self.commandDelegate!.send(pluginResult, callbackId: command!.callbackId)
|
|
127
|
+
}
|
|
128
|
+
return self.isListening;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private func timerCallBack() {
|
|
132
|
+
autoreleasepool {
|
|
133
|
+
if (self.isListening && self.audioRecorder != nil) {
|
|
134
|
+
self.audioRecorder.updateMeters()
|
|
135
|
+
|
|
136
|
+
let peakPowerForChannel = pow(10, (self.audioRecorder.averagePower(forChannel: 0) / 20))
|
|
137
|
+
let db = Int32(round(20 * log10(peakPowerForChannel) + 90))
|
|
138
|
+
|
|
139
|
+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: db)
|
|
140
|
+
pluginResult!.setKeepCallbackAs(true)
|
|
141
|
+
self.commandDelegate!.send(pluginResult, callbackId: self.command.callbackId)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
Convenient method to send plugin errors.
|
|
148
|
+
*/
|
|
149
|
+
private func sendPluginError(callbackId: String, errorCode: PluginError, errorMessage: String) {
|
|
150
|
+
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: ["code": errorCode.hashValue, "message": errorMessage])
|
|
151
|
+
self.commandDelegate!.send(pluginResult, callbackId: callbackId)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
enum PluginError: String {
|
|
155
|
+
case DBMETER_NOT_INITIALIZED = "0"
|
|
156
|
+
case DBMETER_NOT_LISTENING = "1"
|
|
157
|
+
}
|
|
158
|
+
}
|
package/tests/plugin.xml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
|
|
3
|
+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
|
4
|
+
id="cordova-plugin-dbmeter-tests"
|
|
5
|
+
version="1.0.0-dev">
|
|
6
|
+
<name>Cordova DBMeter Plugin Tests</name>
|
|
7
|
+
<license>Apache 2.0</license>
|
|
8
|
+
|
|
9
|
+
<js-module src="tests.js" name="tests">
|
|
10
|
+
</js-module>
|
|
11
|
+
</plugin>
|
package/tests/tests.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
exports.defineAutoTests = function() {
|
|
2
|
+
var fail = function (done) {
|
|
3
|
+
expect(false).toBe(true);
|
|
4
|
+
done();
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
describe('DBMeter (window.DBMeter)', function () {
|
|
8
|
+
it('should exist', function() {
|
|
9
|
+
expect(window.DBMeter).toBeDefined();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should contain a start function', function () {
|
|
13
|
+
expect(typeof window.DBMeter.start).toBeDefined();
|
|
14
|
+
expect(typeof window.DBMeter.start === 'function').toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should contain a stop function', function () {
|
|
18
|
+
expect(typeof window.DBMeter.stop).toBeDefined();
|
|
19
|
+
expect(typeof window.DBMeter.stop === 'function').toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should contain a isListening function', function () {
|
|
23
|
+
expect(typeof window.DBMeter.isListening).toBeDefined();
|
|
24
|
+
expect(typeof window.DBMeter.isListening === 'function').toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should contain a delete function', function () {
|
|
28
|
+
expect(typeof window.DBMeter.delete).toBeDefined();
|
|
29
|
+
expect(typeof window.DBMeter.delete === 'function').toBe(true);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('start method', function () {
|
|
34
|
+
|
|
35
|
+
it('should start the DBMeter', function (done) {
|
|
36
|
+
var started = false;
|
|
37
|
+
|
|
38
|
+
window.DBMeter.start(function() {
|
|
39
|
+
if(!started) {
|
|
40
|
+
started = true;
|
|
41
|
+
window.DBMeter.isListening(function(result) {
|
|
42
|
+
expect(result).toBe(true);
|
|
43
|
+
done();
|
|
44
|
+
}, fail.bind(null, done));
|
|
45
|
+
}
|
|
46
|
+
}, fail.bind(null, done));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should return dB if we start the DBMeter', function (done) {
|
|
50
|
+
var started = false;
|
|
51
|
+
|
|
52
|
+
window.DBMeter.delete(function() {
|
|
53
|
+
window.DBMeter.start(function(dB) {
|
|
54
|
+
if(!started) {
|
|
55
|
+
started = true;
|
|
56
|
+
window.DBMeter.stop(function() {
|
|
57
|
+
expect(dB).not.toBe(null);
|
|
58
|
+
done();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}, fail.bind(null, done));
|
|
62
|
+
}, fail.bind(null, done));
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('stop method', function () {
|
|
67
|
+
|
|
68
|
+
it('should stop the DBMeter', function (done) {
|
|
69
|
+
var started = false;
|
|
70
|
+
|
|
71
|
+
window.DBMeter.delete(function() {
|
|
72
|
+
window.DBMeter.start(function() {
|
|
73
|
+
if(!started) {
|
|
74
|
+
started = true;
|
|
75
|
+
window.DBMeter.stop(function() {
|
|
76
|
+
window.DBMeter.isListening(function(result) {
|
|
77
|
+
expect(result).toBe(false);
|
|
78
|
+
done();
|
|
79
|
+
}, fail.bind(null, done));
|
|
80
|
+
}, fail.bind(null, done));
|
|
81
|
+
}
|
|
82
|
+
}, fail.bind(null, done));
|
|
83
|
+
}, fail.bind(null, done));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('error callback', function () {
|
|
87
|
+
|
|
88
|
+
it('should be called if we stop the DBMeter while it is not started, the expected error should have the code 1', function (done) {
|
|
89
|
+
window.DBMeter.delete(function() {
|
|
90
|
+
window.DBMeter.stop(fail.bind(null, done), function(e) {
|
|
91
|
+
expect(e.code).toBe(1);
|
|
92
|
+
done();
|
|
93
|
+
}, fail.bind(null, done));
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('delete method', function () {
|
|
100
|
+
|
|
101
|
+
it('should delete the DBMeter instance', function (done) {
|
|
102
|
+
var started = false;
|
|
103
|
+
|
|
104
|
+
window.DBMeter.start(function() {
|
|
105
|
+
if(!started) {
|
|
106
|
+
started = true;
|
|
107
|
+
window.DBMeter.isListening(function(result) {
|
|
108
|
+
expect(result).toBe(true);
|
|
109
|
+
window.DBMeter.delete(function() {
|
|
110
|
+
window.DBMeter.isListening(function(result) {
|
|
111
|
+
expect(result).toBe(false);
|
|
112
|
+
done();
|
|
113
|
+
}, fail.bind(null, done));
|
|
114
|
+
}, fail.bind(null, done));
|
|
115
|
+
}, fail.bind(null, done));
|
|
116
|
+
}
|
|
117
|
+
}, fail.bind(null, done));
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('error callback', function () {
|
|
121
|
+
it('should be called if we delete the DBMeter while it is not initialized, the expected error should have the code 0', function (done) {
|
|
122
|
+
window.DBMeter.delete(fail.bind(null, done), function(e) {
|
|
123
|
+
expect(e.code).toBe(0);
|
|
124
|
+
done();
|
|
125
|
+
}, fail.bind(null, done));
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('isListening method', function () {
|
|
131
|
+
|
|
132
|
+
it('should return false if the DBMeter is not initialized', function (done) {
|
|
133
|
+
window.DBMeter.isListening(function(result) {
|
|
134
|
+
expect(result).toBe(false);
|
|
135
|
+
done();
|
|
136
|
+
}, fail.bind(null, done));
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should return true if the DBMeter is listening', function (done) {
|
|
140
|
+
var started = false;
|
|
141
|
+
|
|
142
|
+
window.DBMeter.start(function() {
|
|
143
|
+
if(!started) {
|
|
144
|
+
started = true;
|
|
145
|
+
window.DBMeter.isListening(function(result) {
|
|
146
|
+
expect(result).toBe(true);
|
|
147
|
+
done();
|
|
148
|
+
});
|
|
149
|
+
window.DBMeter.stop();
|
|
150
|
+
window.DBMeter.delete();
|
|
151
|
+
}
|
|
152
|
+
}, fail.bind(null, done));
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
exports.defineManualTests = function(contentEl, createActionButton) {
|
|
158
|
+
|
|
159
|
+
contentEl.innerHTML = '<div class="decibelBarContainer">'
|
|
160
|
+
+ '<div class="decibelBar"></div>'
|
|
161
|
+
+ '</div>'
|
|
162
|
+
+ '<div class="decibel">0dB</div>';
|
|
163
|
+
|
|
164
|
+
var decibelBarContainer = document.querySelector('.decibelBarContainer');
|
|
165
|
+
var decibelBar = document.querySelector('.decibelBar');
|
|
166
|
+
var decibel = document.querySelector('.decibel');
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
decibelBarContainer.style.display = '-webkit-flex';
|
|
170
|
+
decibelBarContainer.style.display = 'flex';
|
|
171
|
+
decibelBarContainer.style.width = '50px';
|
|
172
|
+
decibelBarContainer.style.WebkitTransform = 'translateX(-50%)';
|
|
173
|
+
decibelBarContainer.style.marginLeft = '50%';
|
|
174
|
+
decibelBarContainer.style.position = 'absolute';
|
|
175
|
+
decibelBarContainer.style.top = '140px';
|
|
176
|
+
decibelBarContainer.style.height = '200px';
|
|
177
|
+
|
|
178
|
+
decibel.style.padding = '50px';
|
|
179
|
+
decibel.style.fontSize = '100px';
|
|
180
|
+
decibel.style.WebkitTransform = 'translateX(-50%)';
|
|
181
|
+
decibel.style.marginLeft = '50%';
|
|
182
|
+
decibel.style.position= 'absolute';
|
|
183
|
+
decibel.style.top = '300px';
|
|
184
|
+
decibel.style.textTransform = 'none';
|
|
185
|
+
|
|
186
|
+
decibelBar.style.background ='linear-gradient(to top, #009bca 0px, #009bca 25px, #009182 50px, #009182 100px, #009182 200px)';
|
|
187
|
+
decibelBar.style.flex = '0 1 auto';
|
|
188
|
+
decibelBar.style.alignSelf = 'flex-end';
|
|
189
|
+
decibelBar.style.WebkitFlex = '0 1 auto';
|
|
190
|
+
decibelBar.style.WebkitAlignSelf = 'flex-end';
|
|
191
|
+
decibelBar.style.width = '100%';
|
|
192
|
+
decibelBar.style.height = '2px';
|
|
193
|
+
|
|
194
|
+
createActionButton('Start DBMeter', function() {
|
|
195
|
+
DBMeter.start(function(dB) {
|
|
196
|
+
decibelBar.style.height = parseFloat(dB, 10) + '%';
|
|
197
|
+
decibel.innerHTML = parseInt(dB, 10) + 'dB';
|
|
198
|
+
}, function(e) {
|
|
199
|
+
console.log('code: ' + e.code + ', message: ' + e.message);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
createActionButton('Stop DBMeter', function() {
|
|
204
|
+
DBMeter.stop(function() {
|
|
205
|
+
decibelBar.style.height = '0';
|
|
206
|
+
decibel.innerHTML = '0dB';
|
|
207
|
+
}, function(e) {
|
|
208
|
+
console.log('code: ' + e.code + ', message: ' + e.message);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
};
|