cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shaz...@apache.org
Subject git commit: [CB-4847] iOS 7 microphone access requires user permission - if denied, CDVCapture, CDVSound does not handle it properly
Date Tue, 17 Sep 2013 19:44:34 GMT
Updated Branches:
  refs/heads/dev b67238db0 -> 742069aec


[CB-4847] iOS 7 microphone access requires user permission - if denied, CDVCapture, CDVSound
does not handle it properly


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/commit/742069ae
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/tree/742069ae
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/diff/742069ae

Branch: refs/heads/dev
Commit: 742069aec9d360a1a3f816f9958c5d85fa093864
Parents: b67238d
Author: Shazron Abdullah <shazron@apache.org>
Authored: Tue Sep 17 12:44:20 2013 -0700
Committer: Shazron Abdullah <shazron@apache.org>
Committed: Tue Sep 17 12:44:30 2013 -0700

----------------------------------------------------------------------
 src/ios/CDVSound.m | 114 +++++++++++++++++++++++++++++-------------------
 1 file changed, 69 insertions(+), 45 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/742069ae/src/ios/CDVSound.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVSound.m b/src/ios/CDVSound.m
index ca6985e..ab8f445 100644
--- a/src/ios/CDVSound.m
+++ b/src/ios/CDVSound.m
@@ -527,64 +527,88 @@
 
     NSString* mediaId = [command.arguments objectAtIndex:0];
     CDVAudioFile* audioFile = [self audioFileForResource:[command.arguments objectAtIndex:1]
withId:mediaId doValidation:YES forRecording:YES];
-    NSString* jsString = nil;
-    NSString* errorMsg = @"";
+    __block NSString* jsString = nil;
+    __block NSString* errorMsg = @"";
 
     if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
-        NSError* __autoreleasing error = nil;
-
-        if (audioFile.recorder != nil) {
-            [audioFile.recorder stop];
-            audioFile.recorder = nil;
-        }
-        // get the audioSession and set the category to allow recording when device is locked
or ring/silent switch engaged
-        if ([self hasAudioSession]) {
-            [self.avSession setCategory:AVAudioSessionCategoryRecord error:nil];
-            if (![self.avSession setActive:YES error:&error]) {
-                // other audio with higher priority that does not allow mixing could cause
this to fail
-                errorMsg = [NSString stringWithFormat:@"Unable to record audio: %@", [error
localizedFailureReason]];
-                // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, MEDIA_ERR_ABORTED];
-                jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
-                [self.commandDelegate evalJs:jsString];
-                return;
+        void (^startRecording)(void) = ^{
+            NSError* __autoreleasing error = nil;
+            
+            if (audioFile.recorder != nil) {
+                [audioFile.recorder stop];
+                audioFile.recorder = nil;
             }
-        }
-
-        // create a new recorder for each start record
-        audioFile.recorder = [[CDVAudioRecorder alloc] initWithURL:audioFile.resourceURL
settings:nil error:&error];
-
-        bool recordingSuccess = NO;
-        if (error == nil) {
-            audioFile.recorder.delegate = self;
-            audioFile.recorder.mediaId = mediaId;
-            recordingSuccess = [audioFile.recorder record];
-            if (recordingSuccess) {
-                NSLog(@"Started recording audio sample '%@'", audioFile.resourcePath);
-                jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_STATE, MEDIA_RUNNING];
+            // get the audioSession and set the category to allow recording when device is
locked or ring/silent switch engaged
+            if ([self hasAudioSession]) {
+                [self.avSession setCategory:AVAudioSessionCategoryRecord error:nil];
+                if (![self.avSession setActive:YES error:&error]) {
+                    // other audio with higher priority that does not allow mixing could
cause this to fail
+                    errorMsg = [NSString stringWithFormat:@"Unable to record audio: %@",
[error localizedFailureReason]];
+                    // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, MEDIA_ERR_ABORTED];
+                    jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
+                    [self.commandDelegate evalJs:jsString];
+                    return;
+                }
             }
-        }
-
-        if ((error != nil) || (recordingSuccess == NO)) {
-            if (error != nil) {
-                errorMsg = [NSString stringWithFormat:@"Failed to initialize AVAudioRecorder:
%@\n", [error localizedFailureReason]];
-            } else {
-                errorMsg = @"Failed to start recording using AVAudioRecorder";
+            
+            // create a new recorder for each start record
+            audioFile.recorder = [[CDVAudioRecorder alloc] initWithURL:audioFile.resourceURL
settings:nil error:&error];
+            
+            bool recordingSuccess = NO;
+            if (error == nil) {
+                audioFile.recorder.delegate = self;
+                audioFile.recorder.mediaId = mediaId;
+                recordingSuccess = [audioFile.recorder record];
+                if (recordingSuccess) {
+                    NSLog(@"Started recording audio sample '%@'", audioFile.resourcePath);
+                    jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_STATE, MEDIA_RUNNING];
+                }
             }
-            audioFile.recorder = nil;
-            if (self.avSession) {
-                [self.avSession setActive:NO error:nil];
+            
+            if ((error != nil) || (recordingSuccess == NO)) {
+                if (error != nil) {
+                    errorMsg = [NSString stringWithFormat:@"Failed to initialize AVAudioRecorder:
%@\n", [error localizedFailureReason]];
+                } else {
+                    errorMsg = @"Failed to start recording using AVAudioRecorder";
+                }
+                audioFile.recorder = nil;
+                if (self.avSession) {
+                    [self.avSession setActive:NO error:nil];
+                }
+                jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
             }
-            jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
+        };
+        
+        SEL rrpSel = NSSelectorFromString(@"requestRecordPermission:");
+        if ([self hasAudioSession] && [self.avSession respondsToSelector:rrpSel])
+        {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
+            [self.avSession performSelector:rrpSel withObject:^(BOOL granted){
+                if (granted) {
+                    startRecording();
+                } else {
+                    NSString* msg = @"Error creating audio session, microphone permission
denied.";
+                    NSLog(@"%@", msg);
+                    audioFile.recorder = nil;
+                    if (self.avSession) {
+                        [self.avSession setActive:NO error:nil];
+                    }
+                    jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:msg]];
+                    [self.commandDelegate evalJs:jsString];
+                }
+            }];
+#pragma clang diagnostic pop
+        } else {
+            startRecording();
         }
+        
     } else {
         // file did not validate
         NSString* errorMsg = [NSString stringWithFormat:@"Could not record audio at '%@'",
audioFile.resourcePath];
         jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.media.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
-    }
-    if (jsString) {
         [self.commandDelegate evalJs:jsString];
     }
-    return;
 }
 
 - (void)stopRecordingAudio:(CDVInvokedUrlCommand*)command


Mime
View raw message