Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova 3.3 Pause and Resume recording using Media API

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 !!!

like image 748
madLokesh Avatar asked Jan 12 '23 08:01

madLokesh


1 Answers

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.

like image 141
madLokesh Avatar answered Jan 25 '23 09:01

madLokesh