forked from keenan/cordova-phonegap-audio-encode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AudioEncode.m
80 lines (60 loc) · 2.99 KB
/
AudioEncode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// AudioEncode.m
//
// By Lyle Pratt, September 2011.
// Updated Oct 2012 by Keenan Wyrobek for Cordova 2.1.0
// MIT licensed
//
#import "AudioEncode.h"
@implementation AudioEncode
@synthesize callbackId;
- (void)encodeAudio:(NSArray*)arguments withDict:(NSDictionary*)options
{
self.callbackId = [arguments objectAtIndex:0];
NSString* audioPath = [arguments objectAtIndex:1];
NSURL* audioURL = [NSURL fileURLWithPath:audioPath];
AVURLAsset* audioAsset = [[AVURLAsset alloc] initWithURL:audioURL options:nil];
AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:audioAsset presetName:AVAssetExportPresetAppleM4A];
NSURL* exportURL = [NSURL fileURLWithPath:[[audioPath componentsSeparatedByString:@".wav"] objectAtIndex:0]];
NSURL* destinationURL = [exportURL URLByAppendingPathExtension:@"m4a"];
exportSession.outputURL = destinationURL;
exportSession.outputFileType = AVFileTypeAppleM4A;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (AVAssetExportSessionStatusCompleted == exportSession.status) {
NSLog(@"AVAssetExportSessionStatusCompleted");
[self performSelectorOnMainThread:@selector(doSuccessCallback:) withObject:[exportSession.outputURL path] waitUntilDone:NO];
} else if (AVAssetExportSessionStatusFailed == exportSession.status) {
// a failure may happen because of an event out of your control
// for example, an interruption like a phone call comming in
// make sure and handle this case appropriately
NSLog(@"AVAssetExportSessionStatusFailed");
NSLog(@" error: %@", exportSession.error);
[self performSelectorOnMainThread:@selector(doFailCallback:) withObject:[NSString stringWithFormat:@"%i", exportSession.status] waitUntilDone:NO];
} else {
NSLog(@"Export Session Status: %d", exportSession.status);
}
[exportSession release];
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSError *error = noErr;
if ([fileMgr removeItemAtPath:audioPath error:&error] != YES) {
NSLog(@"Unable to delete file: %@", [error localizedDescription]);
}
}];
}
-(void) doSuccessCallback:(NSString*)path {
NSLog(@"doing success callback");
CDVPluginResult* pluginResult = nil;
NSString* javaScript = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:path];
javaScript = [pluginResult toSuccessCallbackString:self.callbackId];
[self writeJavascript:javaScript];
}
-(void) doFailCallback:(NSString*)status {
NSLog(@"doing fail callback");
CDVPluginResult* pluginResult = nil;
NSString* javaScript = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:status];
javaScript = [pluginResult toErrorCallbackString:self.callbackId];
[self writeJavascript:javaScript];
}
@end