You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2016/04/26 18:15:08 UTC

[2/3] cordova-plugin-media git commit: CB-10776: Add the ability to pause and resume an audio recording (iOS)

CB-10776: Add the ability to pause and resume an audio recording (iOS)


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

Branch: refs/heads/master
Commit: b1ef73d157e6ee56ee235471cc11dca7bcd8af2a
Parents: e4e30ae
Author: Simon MacDonald <si...@gmail.com>
Authored: Wed Apr 6 12:07:36 2016 -0400
Committer: Simon MacDonald <si...@gmail.com>
Committed: Tue Apr 26 12:10:31 2016 -0400

----------------------------------------------------------------------
 README.md          | 86 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/ios/CDVSound.h |  2 ++
 src/ios/CDVSound.m | 41 +++++++++++++++++++++++
 tests/tests.js     | 28 ++++++++++++----
 www/Media.js       | 14 ++++++++
 5 files changed, 164 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/b1ef73d1/README.md
----------------------------------------------------------------------
diff --git a/README.md b/README.md
index 9b71ac0..7912e21 100644
--- a/README.md
+++ b/README.md
@@ -106,8 +106,12 @@ The following constants are reported as the only parameter to the
 
 - `media.pause`: Pause playback of an audio file.
 
+- `media.pauseRecord`: Pause recording of an audio file.
+
 - `media.release`: Releases the underlying operating system's audio resources.
 
+- `media.resumeRecord`: Resume recording of an audio file.
+
 - `media.seekTo`: Moves the position within the audio file.
 
 - `media.setVolume`: Set the volume for audio playback.
@@ -261,6 +265,44 @@ Pauses playing an audio file.
     }
 
 
+## media.pauseRecord
+
+Pauses recording an audio file.
+
+    media.pauseRecord();
+
+
+### Supported Platforms
+
+- iOS
+
+
+### Quick Example
+
+    // Record audio
+    //
+    function recordAudio() {
+        var src = "myrecording.mp3";
+        var mediaRec = new Media(src,
+            // success callback
+            function() {
+                console.log("recordAudio():Audio Success");
+            },
+
+            // error callback
+            function(err) {
+                console.log("recordAudio():Audio Error: "+ err.code);
+            });
+
+        // Record audio
+        mediaRec.startRecord();
+
+        // Pause Recording after 5 seconds
+        setTimeout(function() {
+            my_media.pauseRecord();
+        }, 5000);
+    }
+
 ## media.play
 
 Starts or resumes playing an audio file.
@@ -333,6 +375,50 @@ function for any `Media` resource that is no longer needed.
     my_media.release();
 
 
+## media.resumeRecord
+
+Resume recording an audio file.
+
+    media.resumeRecord();
+
+
+### Supported Platforms
+
+- iOS
+
+
+### Quick Example
+
+    // Record audio
+    //
+    function recordAudio() {
+        var src = "myrecording.mp3";
+        var mediaRec = new Media(src,
+            // success callback
+            function() {
+                console.log("recordAudio():Audio Success");
+            },
+
+            // error callback
+            function(err) {
+                console.log("recordAudio():Audio Error: "+ err.code);
+            });
+
+        // Record audio
+        mediaRec.startRecord();
+
+        // Pause Recording after 5 seconds
+        setTimeout(function() {
+            my_media.pauseRecord();
+        }, 5000);
+
+        // Resume Recording after 10 seconds
+        setTimeout(function() {
+            my_media.resumeRecord();
+        }, 10000);
+    }
+
+
 ## media.seekTo
 
 Sets the current position within an audio file.

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/b1ef73d1/src/ios/CDVSound.h
----------------------------------------------------------------------
diff --git a/src/ios/CDVSound.h b/src/ios/CDVSound.h
index 5e795cc..1dca574 100644
--- a/src/ios/CDVSound.h
+++ b/src/ios/CDVSound.h
@@ -97,6 +97,8 @@ typedef NSUInteger CDVMediaMsg;
 - (void)seekToAudio:(CDVInvokedUrlCommand*)command;
 - (void)release:(CDVInvokedUrlCommand*)command;
 - (void)getCurrentPositionAudio:(CDVInvokedUrlCommand*)command;
+- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command;
+- (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command;
 
 - (BOOL)hasAudioSession;
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/b1ef73d1/src/ios/CDVSound.m
----------------------------------------------------------------------
diff --git a/src/ios/CDVSound.m b/src/ios/CDVSound.m
index 5c3245a..df40d7d 100644
--- a/src/ios/CDVSound.m
+++ b/src/ios/CDVSound.m
@@ -867,6 +867,47 @@
     [self.commandDelegate sendPluginResult:result callbackId:callbackId];
  }
 
+ - (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command
+  {
+     NSString* mediaId = [command argumentAtIndex:0];
+
+     CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
+     NSString* jsString = nil;
+
+     if ((audioFile != nil) && (audioFile.recorder != nil)) {
+         NSLog(@"Resumed recording audio sample '%@'", audioFile.resourcePath);
+         [audioFile.recorder record];
+         // no callback - that will happen in audioRecorderDidFinishRecording
+         jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
+     }
+
+    // ignore if no media recording
+    if (jsString) {
+        [self.commandDelegate evalJs:jsString];
+    }
+}
+
+ - (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command
+  {
+     NSString* mediaId = [command argumentAtIndex:0];
+
+     CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
+     NSString* jsString = nil;
+
+     if ((audioFile != nil) && (audioFile.recorder != nil)) {
+         NSLog(@"Paused recording audio sample '%@'", audioFile.resourcePath);
+         [audioFile.recorder pause];
+         // no callback - that will happen in audioRecorderDidFinishRecording
+         // no callback - that will happen in audioRecorderDidFinishRecording
+         jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_PAUSED];
+     }
+
+      // ignore if no media recording
+      if (jsString) {
+          [self.commandDelegate evalJs:jsString];
+      }
+ }
+
 @end
 
 @implementation CDVAudioFile

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/b1ef73d1/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index b546638..5caba11 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -175,7 +175,21 @@ exports.defineAutoTests = function () {
             media1.release();
         });
 
-        it("media.spec.16 should return MediaError for bad filename", function (done) {
+        it("media.spec.16 should contain a pauseRecord function", function () {
+            var media1 = new Media("dummy");
+            expect(media1.pauseRecord).toBeDefined();
+            expect(typeof media1.pauseRecord).toBe('function');
+            media1.release();
+        });
+
+        it("media.spec.17 should contain a resumeRecord function", function () {
+            var media1 = new Media("dummy");
+            expect(media1.resumeRecord).toBeDefined();
+            expect(typeof media1.resumeRecord).toBe('function');
+            media1.release();
+        });
+
+        it("media.spec.18 should return MediaError for bad filename", function (done) {
             //bb10 dialog pops up, preventing tests from running
             if (cordova.platformId === 'blackberry10') {
                 pending();
@@ -210,7 +224,7 @@ exports.defineAutoTests = function () {
                 }
             });
 
-            it("media.spec.17 position should be set properly", function (done) {
+            it("media.spec.19 position should be set properly", function (done) {
                 // no audio hardware available
                 if (!isAudioSupported) {
                     pending();
@@ -240,7 +254,7 @@ exports.defineAutoTests = function () {
                 media.play();
             }, ACTUAL_PLAYBACK_TEST_TIMEOUT);
 
-            it("media.spec.18 duration should be set properly", function (done) {
+            it("media.spec.20 duration should be set properly", function (done) {
                 if (!isAudioSupported || cordova.platformId === 'blackberry10') {
                     pending();
                 }
@@ -269,7 +283,7 @@ exports.defineAutoTests = function () {
                 media.play();
             }, ACTUAL_PLAYBACK_TEST_TIMEOUT);
 
-            it("media.spec.19 should be able to resume playback after pause", function (done) {
+            it("media.spec.21 should be able to resume playback after pause", function (done) {
                 if (!isAudioSupported || cordova.platformId === 'blackberry10') {
                     pending();
                 }
@@ -312,7 +326,7 @@ exports.defineAutoTests = function () {
 
             }, ACTUAL_PLAYBACK_TEST_TIMEOUT);
 
-            it("media.spec.20 should be able to seek through file", function (done) {
+            it("media.spec.22 should be able to seek through file", function (done) {
                 if (!isAudioSupported || cordova.platformId === 'blackberry10') {
                     pending();
                 }
@@ -346,14 +360,14 @@ exports.defineAutoTests = function () {
             }, ACTUAL_PLAYBACK_TEST_TIMEOUT);
         });
 
-        it("media.spec.21 should contain a setRate function", function () {
+        it("media.spec.23 should contain a setRate function", function () {
             var media1 = new Media("dummy");
             expect(media1.setRate).toBeDefined();
             expect(typeof media1.setRate).toBe('function');
             media1.release();
         });
 
-        it("media.spec.22 playback rate should be set properly using setRate", function (done) {
+        it("media.spec.24 playback rate should be set properly using setRate", function (done) {
             if (cordova.platformId !== 'ios') {
                 expect(true).toFailWithMessage('Platform does not supported this feature');
                 pending();

http://git-wip-us.apache.org/repos/asf/cordova-plugin-media/blob/b1ef73d1/www/Media.js
----------------------------------------------------------------------
diff --git a/www/Media.js b/www/Media.js
index 192d4b3..5806b9f 100644
--- a/www/Media.js
+++ b/www/Media.js
@@ -139,6 +139,20 @@ Media.prototype.stopRecord = function() {
 };
 
 /**
+ * Pause recording audio file.
+ */
+Media.prototype.pauseRecord = function() {
+    exec(null, this.errorCallback, "Media", "pauseRecordingAudio", [this.id]);
+};
+
+/**
+* Resume recording audio file.
+*/
+Media.prototype.resumeRecord = function() {
+    exec(null, this.errorCallback, "Media", "resumeRecordingAudio", [this.id]);
+};
+
+/**
  * Release the resources.
  */
 Media.prototype.release = function() {


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