@@ -22,6 +22,107 @@ public class AudioRecorderAPI extends CordovaPlugin {
22
22
private String outputFile ;
23
23
private CountDownTimer countDowntimer ;
24
24
25
+
26
+ public void onRequestPermissionResult (int requestCode , String [] permissions ,
27
+ int [] grantResults ) throws JSONException
28
+ {
29
+ for (int r :grantResults )
30
+ {
31
+ if (r == PackageManager .PERMISSION_DENIED )
32
+ {
33
+ this .callbackContext .sendPluginResult (new PluginResult (PluginResult .Status .ERROR , PERMISSION_DENIED_ERROR ));
34
+ return ;
35
+ }
36
+ }
37
+ this .record ();
38
+ }
39
+
40
+ public void record () {
41
+ Context context = cordova .getActivity ().getApplicationContext ();
42
+
43
+ outputFile = context .getFilesDir ().getAbsoluteFile () + "/"
44
+ + UUID .randomUUID ().toString () + ".m4a" ;
45
+ myRecorder = new MediaRecorder ();
46
+ myRecorder .setAudioSource (MediaRecorder .AudioSource .MIC );
47
+ myRecorder .setOutputFormat (MediaRecorder .OutputFormat .MPEG_4 );
48
+ myRecorder .setAudioEncoder (MediaRecorder .AudioEncoder .AAC );
49
+ myRecorder .setAudioSamplingRate (44100 );
50
+ myRecorder .setAudioChannels (1 );
51
+ myRecorder .setAudioEncodingBitRate (32000 );
52
+ myRecorder .setOutputFile (outputFile );
53
+
54
+ try {
55
+ myRecorder .prepare ();
56
+ myRecorder .start ();
57
+ } catch (final Exception e ) {
58
+ cordova .getThreadPool ().execute (new Runnable () {
59
+ public void run () {
60
+ callbackContext .error (e .getMessage ());
61
+ }
62
+ });
63
+ return ;
64
+ }
65
+ countDowntimer = new CountDownTimer (seconds * 1000 , 1000 ) {
66
+ public void onTick (long millisUntilFinished ) {}
67
+ public void onFinish () {
68
+ stopRecord (callbackContext );
69
+ }
70
+ };
71
+ countDowntimer .start ();
72
+ }
73
+
74
+ public void requestPermissions (CordovaPlugin plugin , int requestCode , String [] permissions ) {
75
+ try {
76
+ Method requestPermission = CordovaInterface .class .getDeclaredMethod (
77
+ "requestPermissions" , CordovaPlugin .class , int .class , String [].class );
78
+
79
+ // If there is no exception, then this is cordova-android 5.0.0+
80
+ requestPermission .invoke (plugin .cordova , plugin , requestCode , permissions );
81
+ } catch (NoSuchMethodException noSuchMethodException ) {
82
+ // cordova-android version is less than 5.0.0, so permission is implicitly granted
83
+ LOG .d (LOG_TAG , "No need to request permissions " + Arrays .toString (permissions ));
84
+
85
+ // Notify the plugin that all were granted by using more reflection
86
+ deliverPermissionResult (plugin , requestCode , permissions );
87
+ } catch (IllegalAccessException illegalAccessException ) {
88
+ // Should never be caught; this is a public method
89
+ LOG .e (LOG_TAG , "IllegalAccessException when requesting permissions " + Arrays .toString (permissions ), illegalAccessException );
90
+ } catch (InvocationTargetException invocationTargetException ) {
91
+ // This method does not throw any exceptions, so this should never be caught
92
+ LOG .e (LOG_TAG , "invocationTargetException when requesting permissions " + Arrays .toString (permissions ), invocationTargetException );
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Checks at runtime to see if the application has been granted a permission. This is a helper
98
+ * method alternative to cordovaInterface.hasPermission() that does not require the project to
99
+ * be built with cordova-android 5.0.0+
100
+ *
101
+ * @param plugin The plugin the permission is being checked against
102
+ * @param permission The permission to be checked
103
+ *
104
+ * @return True if the permission has already been granted and false otherwise
105
+ */
106
+ public boolean hasPermission (CordovaPlugin plugin , String permission ) {
107
+ try {
108
+ Method hasPermission = CordovaInterface .class .getDeclaredMethod ("hasPermission" , String .class );
109
+
110
+ // If there is no exception, then this is cordova-android 5.0.0+
111
+ return (Boolean ) hasPermission .invoke (plugin .cordova , permission );
112
+ } catch (NoSuchMethodException noSuchMethodException ) {
113
+ // cordova-android version is less than 5.0.0, so permission is implicitly granted
114
+ LOG .d (LOG_TAG , "No need to check for permission " + permission );
115
+ return true ;
116
+ } catch (IllegalAccessException illegalAccessException ) {
117
+ // Should never be caught; this is a public method
118
+ LOG .e (LOG_TAG , "IllegalAccessException when checking permission " + permission , illegalAccessException );
119
+ } catch (InvocationTargetException invocationTargetException ) {
120
+ // This method does not throw any exceptions, so this should never be caught
121
+ LOG .e (LOG_TAG , "invocationTargetException when checking permission " + permission , invocationTargetException );
122
+ }
123
+ return false ;
124
+ }
125
+
25
126
@ Override
26
127
public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
27
128
Context context = cordova .getActivity ().getApplicationContext ();
@@ -32,36 +133,12 @@ public boolean execute(String action, JSONArray args, final CallbackContext call
32
133
seconds = 7 ;
33
134
}
34
135
if (action .equals ("record" )) {
35
- outputFile = context .getFilesDir ().getAbsoluteFile () + "/"
36
- + UUID .randomUUID ().toString () + ".m4a" ;
37
- myRecorder = new MediaRecorder ();
38
- myRecorder .setAudioSource (MediaRecorder .AudioSource .MIC );
39
- myRecorder .setOutputFormat (MediaRecorder .OutputFormat .MPEG_4 );
40
- myRecorder .setAudioEncoder (MediaRecorder .AudioEncoder .AAC );
41
- myRecorder .setAudioSamplingRate (44100 );
42
- myRecorder .setAudioChannels (1 );
43
- myRecorder .setAudioEncodingBitRate (32000 );
44
- myRecorder .setOutputFile (outputFile );
45
-
46
- try {
47
- myRecorder .prepare ();
48
- myRecorder .start ();
49
- } catch (final Exception e ) {
50
- cordova .getThreadPool ().execute (new Runnable () {
51
- public void run () {
52
- callbackContext .error (e .getMessage ());
53
- }
54
- });
55
- return false ;
136
+ if (!this .hasPermission (this , permissions [0 ])) {
137
+ String [] permissions = { };
138
+ this .requestPermission (this , 0 , new String [] {"android.permission.RECORD_AUDIO" });
139
+ } else {
140
+ this .record ();
56
141
}
57
-
58
- countDowntimer = new CountDownTimer (seconds * 1000 , 1000 ) {
59
- public void onTick (long millisUntilFinished ) {}
60
- public void onFinish () {
61
- stopRecord (callbackContext );
62
- }
63
- };
64
- countDowntimer .start ();
65
142
return true ;
66
143
}
67
144
0 commit comments