cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject [1/7] git commit: [CB-4847] iOS 7 microphone access requires user permission - if denied, CDVCapture, CDVSound does not handle it properly
Date Mon, 28 Oct 2013 22:13:06 GMT
Updated Branches:
  refs/heads/dev d592158d8 -> 5784fdaa8


[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/0259d766
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/tree/0259d766
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/diff/0259d766

Branch: refs/heads/dev
Commit: 0259d766615724531a961058b591bc5903fc6c68
Parents: b5f24d7
Author: Shazron Abdullah <shazron@apache.org>
Authored: Mon Sep 16 16:43:50 2013 -0700
Committer: Shazron Abdullah <shazron@apache.org>
Committed: Mon Sep 16 16:43:50 2013 -0700

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


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/0259d766/src/ios/CDVSound.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVSound.m b/src/ios/CDVSound.m
index 5e9fd25..17ead13 100644
--- a/src/ios/CDVSound.m
+++ b/src/ios/CDVSound.m
@@ -527,64 +527,91 @@
 
     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.AudioHandler.Media').onStatus",
mediaId, MEDIA_ERROR, MEDIA_ERR_ABORTED];
-                jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.AudioHandler.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.AudioHandler.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.AudioHandler.Media').onStatus",
mediaId, MEDIA_ERROR, MEDIA_ERR_ABORTED];
+                    jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.AudioHandler.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.AudioHandler.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.AudioHandler.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
             }
-            jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.core.AudioHandler.Media').onStatus",
mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_ABORTED message:errorMsg]];
+            if (jsString) {
+                [self.commandDelegate evalJs:jsString];
+            }
+        };
+        
+        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.AudioHandler.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.AudioHandler.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