Skip to content

Commit 0ab00a0

Browse files
author
xwang
committed
add duration of audio to callback.
1 parent 1013c62 commit 0ab00a0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/android/com/emj365/plugins/AudioRecorderAPI.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.cordova.CordovaPlugin;
55
import org.apache.cordova.PluginResult;
66
import org.json.JSONArray;
7+
import org.json.JSONObject;
78
import org.json.JSONException;
89
import android.media.MediaRecorder;
910
import android.media.MediaPlayer;
@@ -21,11 +22,12 @@ public class AudioRecorderAPI extends CordovaPlugin {
2122
private MediaRecorder myRecorder;
2223
private String outputFile;
2324
private CountDownTimer countDowntimer;
25+
private Integer seconds;
26+
private Integer duration;
2427

2528
@Override
2629
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
2730
Context context = cordova.getActivity().getApplicationContext();
28-
Integer seconds;
2931
if (args.length() >= 1) {
3032
seconds = args.getInt(0);
3133
} else {
@@ -56,7 +58,9 @@ public void run() {
5658
}
5759

5860
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+
}
6064
public void onFinish() {
6165
stopRecord(callbackContext);
6266
}
@@ -110,9 +114,18 @@ private void stopRecord(final CallbackContext callbackContext) {
110114
myRecorder.release();
111115
cordova.getThreadPool().execute(new Runnable() {
112116
public void run() {
113-
callbackContext.success(outputFile);
117+
try {
118+
callbackContext.success(composeCallback());
119+
} catch (JSONException e) {
120+
callbackContext.error(e.getMessage());
121+
}
114122
}
115123
});
116124
}
117125

126+
private JSONObject composeCallback() throws JSONException {
127+
String json = "{ path: '" + outputFile + "', duration: " + duration + " }";
128+
return new JSONObject(json);
129+
}
130+
118131
}

src/ios/AudioRecorderAPI.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully
9797
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
9898
NSError *err = nil;
9999
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
100+
AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:recorderFilePath] options:nil];
101+
int audioDuration = CMTimeGetSeconds(audioAsset.duration);
100102
if(!audioData) {
101103
NSLog(@"audio data: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
102104
} else {
103-
NSLog(@"recording saved: %@", recorderFilePath);
104-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:recorderFilePath];
105+
NSLog(@"recording saved: %@ %d", recorderFilePath, audioDuration);
106+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"path": recorderFilePath, @"duration": [NSNumber numberWithInt:audioDuration]}];
105107
[self.commandDelegate sendPluginResult:pluginResult callbackId:_command.callbackId];
106108
}
107109
}

0 commit comments

Comments
 (0)