4
4
import org .apache .cordova .CordovaPlugin ;
5
5
import org .apache .cordova .PluginResult ;
6
6
import org .json .JSONArray ;
7
+ import org .json .JSONObject ;
7
8
import org .json .JSONException ;
8
9
import android .media .MediaRecorder ;
9
10
import android .media .MediaPlayer ;
@@ -21,11 +22,12 @@ public class AudioRecorderAPI extends CordovaPlugin {
21
22
private MediaRecorder myRecorder ;
22
23
private String outputFile ;
23
24
private CountDownTimer countDowntimer ;
25
+ private Integer seconds ;
26
+ private Integer duration ;
24
27
25
28
@ Override
26
29
public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
27
30
Context context = cordova .getActivity ().getApplicationContext ();
28
- Integer seconds ;
29
31
if (args .length () >= 1 ) {
30
32
seconds = args .getInt (0 );
31
33
} else {
@@ -56,7 +58,9 @@ public void run() {
56
58
}
57
59
58
60
countDowntimer = new CountDownTimer (seconds * 1000 , 1000 ) {
59
- public void onTick (long millisUntilFinished ) {}
61
+ public void onTick (long millisUntilFinished ) {
62
+ duration = seconds - (int ) millisUntilFinished / 1000 ;
63
+ }
60
64
public void onFinish () {
61
65
stopRecord (callbackContext );
62
66
}
@@ -110,9 +114,18 @@ private void stopRecord(final CallbackContext callbackContext) {
110
114
myRecorder .release ();
111
115
cordova .getThreadPool ().execute (new Runnable () {
112
116
public void run () {
113
- callbackContext .success (outputFile );
117
+ try {
118
+ callbackContext .success (composeCallback ());
119
+ } catch (JSONException e ) {
120
+ callbackContext .error (e .getMessage ());
121
+ }
114
122
}
115
123
});
116
124
}
117
125
126
+ private JSONObject composeCallback () throws JSONException {
127
+ String json = "{ path: '" + outputFile + "', duration: " + duration + " }" ;
128
+ return new JSONObject (json );
129
+ }
130
+
118
131
}
0 commit comments