Skip to content

Commit b1ef73d

Browse files
committed
CB-10776: Add the ability to pause and resume an audio recording (iOS)
1 parent e4e30ae commit b1ef73d

File tree

5 files changed

+164
-7
lines changed

5 files changed

+164
-7
lines changed

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ The following constants are reported as the only parameter to the
106106

107107
- `media.pause`: Pause playback of an audio file.
108108

109+
- `media.pauseRecord`: Pause recording of an audio file.
110+
109111
- `media.release`: Releases the underlying operating system's audio resources.
110112

113+
- `media.resumeRecord`: Resume recording of an audio file.
114+
111115
- `media.seekTo`: Moves the position within the audio file.
112116

113117
- `media.setVolume`: Set the volume for audio playback.
@@ -261,6 +265,44 @@ Pauses playing an audio file.
261265
}
262266

263267

268+
## media.pauseRecord
269+
270+
Pauses recording an audio file.
271+
272+
media.pauseRecord();
273+
274+
275+
### Supported Platforms
276+
277+
- iOS
278+
279+
280+
### Quick Example
281+
282+
// Record audio
283+
//
284+
function recordAudio() {
285+
var src = "myrecording.mp3";
286+
var mediaRec = new Media(src,
287+
// success callback
288+
function() {
289+
console.log("recordAudio():Audio Success");
290+
},
291+
292+
// error callback
293+
function(err) {
294+
console.log("recordAudio():Audio Error: "+ err.code);
295+
});
296+
297+
// Record audio
298+
mediaRec.startRecord();
299+
300+
// Pause Recording after 5 seconds
301+
setTimeout(function() {
302+
my_media.pauseRecord();
303+
}, 5000);
304+
}
305+
264306
## media.play
265307

266308
Starts or resumes playing an audio file.
@@ -333,6 +375,50 @@ function for any `Media` resource that is no longer needed.
333375
my_media.release();
334376

335377

378+
## media.resumeRecord
379+
380+
Resume recording an audio file.
381+
382+
media.resumeRecord();
383+
384+
385+
### Supported Platforms
386+
387+
- iOS
388+
389+
390+
### Quick Example
391+
392+
// Record audio
393+
//
394+
function recordAudio() {
395+
var src = "myrecording.mp3";
396+
var mediaRec = new Media(src,
397+
// success callback
398+
function() {
399+
console.log("recordAudio():Audio Success");
400+
},
401+
402+
// error callback
403+
function(err) {
404+
console.log("recordAudio():Audio Error: "+ err.code);
405+
});
406+
407+
// Record audio
408+
mediaRec.startRecord();
409+
410+
// Pause Recording after 5 seconds
411+
setTimeout(function() {
412+
my_media.pauseRecord();
413+
}, 5000);
414+
415+
// Resume Recording after 10 seconds
416+
setTimeout(function() {
417+
my_media.resumeRecord();
418+
}, 10000);
419+
}
420+
421+
336422
## media.seekTo
337423

338424
Sets the current position within an audio file.

src/ios/CDVSound.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ typedef NSUInteger CDVMediaMsg;
9797
- (void)seekToAudio:(CDVInvokedUrlCommand*)command;
9898
- (void)release:(CDVInvokedUrlCommand*)command;
9999
- (void)getCurrentPositionAudio:(CDVInvokedUrlCommand*)command;
100+
- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command;
101+
- (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command;
100102

101103
- (BOOL)hasAudioSession;
102104

src/ios/CDVSound.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,47 @@ - (void)getCurrentAmplitudeAudio:(CDVInvokedUrlCommand*)command
867867
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
868868
}
869869

870+
- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command
871+
{
872+
NSString* mediaId = [command argumentAtIndex:0];
873+
874+
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
875+
NSString* jsString = nil;
876+
877+
if ((audioFile != nil) && (audioFile.recorder != nil)) {
878+
NSLog(@"Resumed recording audio sample '%@'", audioFile.resourcePath);
879+
[audioFile.recorder record];
880+
// no callback - that will happen in audioRecorderDidFinishRecording
881+
jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
882+
}
883+
884+
// ignore if no media recording
885+
if (jsString) {
886+
[self.commandDelegate evalJs:jsString];
887+
}
888+
}
889+
890+
- (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command
891+
{
892+
NSString* mediaId = [command argumentAtIndex:0];
893+
894+
CDVAudioFile* audioFile = [[self soundCache] objectForKey:mediaId];
895+
NSString* jsString = nil;
896+
897+
if ((audioFile != nil) && (audioFile.recorder != nil)) {
898+
NSLog(@"Paused recording audio sample '%@'", audioFile.resourcePath);
899+
[audioFile.recorder pause];
900+
// no callback - that will happen in audioRecorderDidFinishRecording
901+
// no callback - that will happen in audioRecorderDidFinishRecording
902+
jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%d);", @"cordova.require('cordova-plugin-media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_PAUSED];
903+
}
904+
905+
// ignore if no media recording
906+
if (jsString) {
907+
[self.commandDelegate evalJs:jsString];
908+
}
909+
}
910+
870911
@end
871912

872913
@implementation CDVAudioFile

tests/tests.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,21 @@ exports.defineAutoTests = function () {
175175
media1.release();
176176
});
177177

178-
it("media.spec.16 should return MediaError for bad filename", function (done) {
178+
it("media.spec.16 should contain a pauseRecord function", function () {
179+
var media1 = new Media("dummy");
180+
expect(media1.pauseRecord).toBeDefined();
181+
expect(typeof media1.pauseRecord).toBe('function');
182+
media1.release();
183+
});
184+
185+
it("media.spec.17 should contain a resumeRecord function", function () {
186+
var media1 = new Media("dummy");
187+
expect(media1.resumeRecord).toBeDefined();
188+
expect(typeof media1.resumeRecord).toBe('function');
189+
media1.release();
190+
});
191+
192+
it("media.spec.18 should return MediaError for bad filename", function (done) {
179193
//bb10 dialog pops up, preventing tests from running
180194
if (cordova.platformId === 'blackberry10') {
181195
pending();
@@ -210,7 +224,7 @@ exports.defineAutoTests = function () {
210224
}
211225
});
212226

213-
it("media.spec.17 position should be set properly", function (done) {
227+
it("media.spec.19 position should be set properly", function (done) {
214228
// no audio hardware available
215229
if (!isAudioSupported) {
216230
pending();
@@ -240,7 +254,7 @@ exports.defineAutoTests = function () {
240254
media.play();
241255
}, ACTUAL_PLAYBACK_TEST_TIMEOUT);
242256

243-
it("media.spec.18 duration should be set properly", function (done) {
257+
it("media.spec.20 duration should be set properly", function (done) {
244258
if (!isAudioSupported || cordova.platformId === 'blackberry10') {
245259
pending();
246260
}
@@ -269,7 +283,7 @@ exports.defineAutoTests = function () {
269283
media.play();
270284
}, ACTUAL_PLAYBACK_TEST_TIMEOUT);
271285

272-
it("media.spec.19 should be able to resume playback after pause", function (done) {
286+
it("media.spec.21 should be able to resume playback after pause", function (done) {
273287
if (!isAudioSupported || cordova.platformId === 'blackberry10') {
274288
pending();
275289
}
@@ -312,7 +326,7 @@ exports.defineAutoTests = function () {
312326

313327
}, ACTUAL_PLAYBACK_TEST_TIMEOUT);
314328

315-
it("media.spec.20 should be able to seek through file", function (done) {
329+
it("media.spec.22 should be able to seek through file", function (done) {
316330
if (!isAudioSupported || cordova.platformId === 'blackberry10') {
317331
pending();
318332
}
@@ -346,14 +360,14 @@ exports.defineAutoTests = function () {
346360
}, ACTUAL_PLAYBACK_TEST_TIMEOUT);
347361
});
348362

349-
it("media.spec.21 should contain a setRate function", function () {
363+
it("media.spec.23 should contain a setRate function", function () {
350364
var media1 = new Media("dummy");
351365
expect(media1.setRate).toBeDefined();
352366
expect(typeof media1.setRate).toBe('function');
353367
media1.release();
354368
});
355369

356-
it("media.spec.22 playback rate should be set properly using setRate", function (done) {
370+
it("media.spec.24 playback rate should be set properly using setRate", function (done) {
357371
if (cordova.platformId !== 'ios') {
358372
expect(true).toFailWithMessage('Platform does not supported this feature');
359373
pending();

www/Media.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ Media.prototype.stopRecord = function() {
138138
exec(null, this.errorCallback, "Media", "stopRecordingAudio", [this.id]);
139139
};
140140

141+
/**
142+
* Pause recording audio file.
143+
*/
144+
Media.prototype.pauseRecord = function() {
145+
exec(null, this.errorCallback, "Media", "pauseRecordingAudio", [this.id]);
146+
};
147+
148+
/**
149+
* Resume recording audio file.
150+
*/
151+
Media.prototype.resumeRecord = function() {
152+
exec(null, this.errorCallback, "Media", "resumeRecordingAudio", [this.id]);
153+
};
154+
141155
/**
142156
* Release the resources.
143157
*/

0 commit comments

Comments
 (0)