cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From surajpindo...@apache.org
Subject [cordova-plugin-media] 04/06: CB-7684 (#143)
Date Thu, 25 Jan 2018 19:54:13 GMT
This is an automated email from the ASF dual-hosted git repository.

surajpindoria pushed a commit to branch 5.0.x
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-media.git

commit 3d794ca9401ad6cce09679dfc5e53e0b184d4e95
Author: Adriano D.G <me@adrianodigiovanni.com>
AuthorDate: Mon Jan 8 22:14:23 2018 +0100

    CB-7684 (#143)
    
    * do not deactivate avsession when items finish playing
    
    * CB76-84: check if any audio file is playing or recording before deactivating the avsession
    
    * CB-7684: add ios preference to keep avsession always active
    
    * CB-7684: change variable name to uppercase
    
    * CB-7684: change variable name to uppercase in CDVSound.m, too
    
    * CB-7684: fix plugin definition
    
    * CB-7684: activate avsession during plugin initialization
    
    * update plugin definition to solve merge conflict
    
    * update plugin definition to fix merge conflict
    
    * update plugin definition to fix merge conflict
---
 plugin.xml         |  4 ++++
 src/ios/CDVSound.m | 58 +++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/plugin.xml b/plugin.xml
index e05aa07..564f6da 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -66,10 +66,14 @@ id="cordova-plugin-media"
 
     <!-- ios -->
     <platform name="ios">
+      <preference name="KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" default="NO" />
         <config-file target="config.xml" parent="/*">
             <feature name="Media">
                 <param name="ios-package" value="CDVSound" />
             </feature>
+            <preference
+              name="KeepAVAudioSessionAlwaysActive"
+              value="$KEEP_AVAUDIOSESSION_ALWAYS_ACTIVE" />
         </config-file>
         <header-file src="src/ios/CDVSound.h" />
         <source-file src="src/ios/CDVSound.m" />
diff --git a/src/ios/CDVSound.m b/src/ios/CDVSound.m
index 474ed97..7a00ead 100644
--- a/src/ios/CDVSound.m
+++ b/src/ios/CDVSound.m
@@ -27,8 +27,24 @@
 
 @implementation CDVSound
 
+BOOL keepAvAudioSessionAlwaysActive = NO;
+
 @synthesize soundCache, avSession, currMediaId, statusCallbackId;
 
+-(void) pluginInitialize
+{
+    NSDictionary* settings = self.commandDelegate.settings;
+    keepAvAudioSessionAlwaysActive = [[settings objectForKey:[@"KeepAVAudioSessionAlwaysActive"
lowercaseString]] boolValue];
+    if (keepAvAudioSessionAlwaysActive) {
+        if ([self hasAudioSession]) {
+            NSError* error = nil;
+            if(![self.avSession setActive:YES error:&error]) {
+                NSLog(@"Unable to activate session: %@", [error localizedFailureReason]);
+            }
+        }
+    }
+}
+
 // Maps a url for a resource path for recording
 - (NSURL*)urlForRecording:(NSString*)resourcePath
 {
@@ -421,7 +437,7 @@
                 [audioFile.player play];
             } */
             // error creating the session or player
-            [self onStatus:MEDIA_ERROR mediaId:mediaId 
+            [self onStatus:MEDIA_ERROR mediaId:mediaId
               param:[self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
         }
     }
@@ -469,7 +485,7 @@
     if (playerError != nil) {
         NSLog(@"Failed to initialize AVAudioPlayer: %@\n", [playerError localizedDescription]);
         audioFile.player = nil;
-        if (self.avSession) {
+        if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self
isPlayingOrRecording]) {
             [self.avSession setActive:NO error:nil];
         }
         bError = YES;
@@ -600,7 +616,7 @@
                 [avPlayer pause];
                 avPlayer = nil;
             }
-            if (self.avSession) {
+            if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self
isPlayingOrRecording]) {
                 [self.avSession setActive:NO error:nil];
                 self.avSession = nil;
             }
@@ -705,7 +721,7 @@
                     errorMsg = @"Failed to start recording using AVAudioRecorder";
                 }
                 audioFile.recorder = nil;
-                if (weakSelf.avSession) {
+                if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession &&
! [self isPlayingOrRecording]) {
                     [weakSelf.avSession setActive:NO error:nil];
                 }
                 [weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
@@ -725,7 +741,7 @@
                     NSString* msg = @"Error creating audio session, microphone permission
denied.";
                     NSLog(@"%@", msg);
                     audioFile.recorder = nil;
-                    if (weakSelf.avSession) {
+                    if (! keepAvAudioSessionAlwaysActive && weakSelf.avSession &&
! [self isPlayingOrRecording]) {
                         [weakSelf.avSession setActive:NO error:nil];
                     }
                     [weakSelf onStatus:MEDIA_ERROR mediaId:mediaId param:
@@ -773,7 +789,7 @@
         [self onStatus:MEDIA_ERROR mediaId:mediaId param:
           [self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
     }
-    if (self.avSession) {
+    if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording])
{
         [self.avSession setActive:NO error:nil];
     }
 }
@@ -795,18 +811,18 @@
         [self onStatus:MEDIA_ERROR mediaId:mediaId param:
             [self createMediaErrorWithCode:MEDIA_ERR_DECODE message:nil]];
     }
-    if (self.avSession) {
-        [self.avSession setActive:NO error:nil];
-    }
+     if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording])
{
+         [self.avSession setActive:NO error:nil];
+     }
 }
 
 -(void)itemDidFinishPlaying:(NSNotification *) notification {
     // Will be called when AVPlayer finishes playing playerItem
     NSString* mediaId = self.currMediaId;
 
-    if (self.avSession) {
-        [self.avSession setActive:NO error:nil];
-    }
+     if (! keepAvAudioSessionAlwaysActive && self.avSession && ! [self isPlayingOrRecording])
{
+         [self.avSession setActive:NO error:nil];
+     }
     [self onStatus:MEDIA_STATE mediaId:mediaId param:@(MEDIA_STOPPED)];
 }
 
@@ -935,7 +951,7 @@
         status[@"msgType"] = @(what);
         //in the error case contains a dict with "code" and "message"
         //otherwise a NSNumber
-        status[@"value"] = param; 
+        status[@"value"] = param;
         status[@"id"] = mediaId;
         NSMutableDictionary* dict=[NSMutableDictionary dictionary];
         dict[@"action"] = @"status";
@@ -949,12 +965,26 @@
             param=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
         }
         NSString* jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);",
-              @"cordova.require('cordova-plugin-media.Media').onStatus", 
+              @"cordova.require('cordova-plugin-media.Media').onStatus",
               mediaId, (int)what, param];
         [self.commandDelegate evalJs:jsString];
     }
 }
 
+-(BOOL) isPlayingOrRecording
+{
+    for(NSString* mediaId in soundCache) {
+        CDVAudioFile* audioFile = [soundCache objectForKey:mediaId];
+        if (audioFile.player && [audioFile.player isPlaying]) {
+            return true;
+        }
+        if (audioFile.recorder && [audioFile.recorder isRecording]) {
+            return true;
+        }
+    }
+    return false;
+}
+
 @end
 
 @implementation CDVAudioFile

-- 
To stop receiving notification emails like this one, please contact
surajpindoria@apache.org.

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message