Skip to content

Commit 7c19297

Browse files
committed
add palyback method in iOS part
1 parent 6e8c4cb commit 7c19297

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

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 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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ - (void)stop:(CDVInvokedUrlCommand*)command {
6969
NSLog(@"stopped");
7070
}
7171

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"];
93+
[self.commandDelegate sendPluginResult:pluginResult callbackId:_command.callbackId];
94+
}
95+
7296
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
7397
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
7498
NSError *err = nil;

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)