Skip to content

Commit 99d8f30

Browse files
committed
Merge branch 'release/v0.0.4'
2 parents 0ed47ad + f7b5c08 commit 99d8f30

File tree

6 files changed

+85
-10
lines changed

6 files changed

+85
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ This plugin is a Cordova audio recorder plugin which works as API.
88

99
Different than http://plugins.cordova.io/#/package/org.apache.cordova.media-capture this plugin doesn't request the native recorder app (system default recorder) and active recording manually.
1010

11+
Supports platforms:
12+
--------------------
13+
14+
- iOS
15+
- Android
16+
1117
Install:
1218
---------
1319

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.0.3",
2+
"version": "0.0.4",
33
"name": "cordova-plugin-audio-recorder-api",
44
"cordova_name": "AudioRecorderAPI",
55
"description": "This plugin is a Cordova audio recorder plugin which works as API.",

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import org.json.JSONArray;
77
import org.json.JSONException;
88
import android.media.MediaRecorder;
9+
import android.media.MediaPlayer;
10+
import android.media.AudioManager;
911
import android.os.CountDownTimer;
1012
import android.os.Environment;
1113
import android.content.Context;
1214
import java.util.UUID;
15+
import java.io.FileInputStream;
16+
import java.io.File;
17+
import java.io.IOException;
1318

1419
public class AudioRecorderAPI extends CordovaPlugin {
1520

@@ -66,6 +71,37 @@ public void onFinish() {
6671
return true;
6772
}
6873

74+
if (action.equals("playback")) {
75+
MediaPlayer mp = new MediaPlayer();
76+
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
77+
try {
78+
FileInputStream fis = new FileInputStream(new File(outputFile));
79+
mp.setDataSource(fis.getFD());
80+
} catch (IllegalArgumentException e) {
81+
e.printStackTrace();
82+
} catch (SecurityException e) {
83+
e.printStackTrace();
84+
} catch (IllegalStateException e) {
85+
e.printStackTrace();
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
}
89+
try {
90+
mp.prepare();
91+
} catch (IllegalStateException e) {
92+
e.printStackTrace();
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
97+
public void onCompletion(MediaPlayer mp) {
98+
callbackContext.success("playbackComplete");
99+
}
100+
});
101+
mp.start();
102+
return true;
103+
}
104+
69105
return false;
70106
}
71107

src/ios/AudioRecorderAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
NSString *recorderFilePath;
66
NSNumber *duration;
77
AVAudioRecorder *recorder;
8+
AVAudioPlayer *player;
89
CDVPluginResult *pluginResult;
910
CDVInvokedUrlCommand *_command;
1011
}
1112

12-
@property(assign) id<AVAudioRecorderDelegate> delegate;
13-
1413
- (void)record:(CDVInvokedUrlCommand*)command;
1514
- (void)stop:(CDVInvokedUrlCommand*)command;
15+
- (void)playback:(CDVInvokedUrlCommand*)command;
1616

1717
@end

src/ios/AudioRecorderAPI.m

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ - (void)record:(CDVInvokedUrlCommand*)command {
3636

3737
// Create a new dated file
3838
NSString *uuid = [[NSUUID UUID] UUIDString];
39-
recorderFilePath = [NSString stringWithFormat:@"%@/%@.caf", RECORDINGS_FOLDER, uuid];
39+
recorderFilePath = [NSString stringWithFormat:@"%@/%@.m4a", RECORDINGS_FOLDER, uuid];
4040
NSLog(@"recording file path: %@", recorderFilePath);
4141

4242
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
@@ -62,19 +62,48 @@ - (void)record:(CDVInvokedUrlCommand*)command {
6262
}];
6363
}
6464

65-
- (void)stop:(CDVInvokedUrlCommand*)command
66-
{
65+
- (void)stop:(CDVInvokedUrlCommand*)command {
6766
_command = command;
6867
NSLog(@"stopRecording");
6968
[recorder stop];
7069
NSLog(@"stopped");
7170
}
7271

73-
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
74-
{
75-
NSLog(@"recording saved: %@", recorderFilePath);
76-
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:recorderFilePath];
72+
- (void)playback:(CDVInvokedUrlCommand*)command {
73+
_command = command;
74+
[self.commandDelegate runInBackground:^{
75+
NSLog(@"recording playback");
76+
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
77+
NSError *err;
78+
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
79+
player.numberOfLoops = 0;
80+
player.delegate = self;
81+
[player prepareToPlay];
82+
[player play];
83+
if (err) {
84+
NSLog(@"%@ %d %@", [err domain], [err code], [[err userInfo] description]);
85+
}
86+
NSLog(@"playing");
87+
}];
88+
}
89+
90+
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
91+
NSLog(@"audioPlayerDidFinishPlaying");
92+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"playbackComplete"];
7793
[self.commandDelegate sendPluginResult:pluginResult callbackId:_command.callbackId];
7894
}
7995

96+
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
97+
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
98+
NSError *err = nil;
99+
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
100+
if(!audioData) {
101+
NSLog(@"audio data: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
102+
} else {
103+
NSLog(@"recording saved: %@", recorderFilePath);
104+
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:recorderFilePath];
105+
[self.commandDelegate sendPluginResult:pluginResult callbackId:_command.callbackId];
106+
}
107+
}
108+
80109
@end

www/AudioRecorderAPI.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ AudioRecorderAPI.prototype.stop = function (successCallback, errorCallback) {
99
cordova.exec(successCallback, errorCallback, "AudioRecorderAPI", "stop", []);
1010
};
1111

12+
AudioRecorderAPI.prototype.playback = function (successCallback, errorCallback) {
13+
cordova.exec(successCallback, errorCallback, "AudioRecorderAPI", "playback", []);
14+
};
15+
1216
AudioRecorderAPI.install = function () {
1317
if (!window.plugins) {
1418
window.plugins = {};

0 commit comments

Comments
 (0)