4

I am making a simple recorder for iOS 5.0+ using Cordova's Media API. I am giving the user ability to start-pause-resume-stop recording an audio.

The three buttons that I have defined are

Start Recording

Stop Recording

Pause/Resume Recording

I am able to successfully start & stop a recording. What I am unable to do is pause a recording and then resume it again.

I referred to the Media API examples of Cordova and have used somewhat as is in my code as well.

Kindly help !!!

4
  • I don't think there is a pauseRecord() method...you would probably have to write this yourself either as an additional plugin or hack on the original plugin. You could also possibly use the Capture API to bring up the devices' native audio recording app which would probably have a pauseRecord function: github.com/apache/cordova-plugin-media-capture/blob/master/doc/…
    – MBillau
    Jan 27, 2014 at 13:43
  • My app requirement is such that I cannot use native recorder. I have to use a custom UI for the recorder thats why I am using Media API
    – madLokesh
    Jan 28, 2014 at 11:38
  • 1
    I think you will have to create your own plugin then or exend the media plugin. Although a quick google-fu seems to indicate that you can't pause/resume audio recording in Android natively anyway - not sure about iOS. You might need to figure something out with just starting/stopping the recording.
    – MBillau
    Jan 28, 2014 at 15:02
  • Thanks @MBillau. Your suggestion worked and I extended the Media Plugin to use pause and resume functionalities of recorder. I have added an answer as well. plz upvote my question and answer if you think its helpful.
    – madLokesh
    Jan 30, 2014 at 8:53

1 Answer 1

9

I was able to invoke the pause and resume functionalities of recorder using Media API by extending the Media API plugin.

Following is the solution of the same:

NATIVE PART OF MEDIA API PLUGIN

Add the following methods to CDVSound.m

CDVSound.m

- (void)resumeRecordingAudio:(CDVInvokedUrlCommand*)command
 {
    NSString* mediaId = [command.arguments objectAtIndex: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
    }
    // ignore if no media recording
    if (jsString) {
        [self.commandDelegate evalJs:jsString];
    }
}

- (void)pauseRecordingAudio:(CDVInvokedUrlCommand*)command
 {
    NSString* mediaId = [command.arguments objectAtIndex: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
    }
    // ignore if no media recording
    if (jsString) {
        [self.commandDelegate evalJs:jsString];
    }
}

JAVASCRIPT PART OF MEDIA API PLUGIN

Add the following code to the Media.js file under www folder of org.apache.cordova.media.

make sure you add the code below the Media.prototype.startRecord 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]);
};

Once you have extended the plugin, you simply have to call nameOfRecorder.pauseRecord(); and nameOfRecorder.resumeRecord(); as per your needs and requirements.

PS: I am using Cordova 3.3 with XCode 5.0.2

Hope this helps.

2
  • 1
    This is really helpful for me too. btw is there a solution for Android plugin? Oct 5, 2015 at 14:28
  • @WangYinXing I am so sorry but I haven't tried on Android . Also, I am not into hybrid development for the past few months now.
    – madLokesh
    Oct 6, 2015 at 9:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.