how to pause and resume voice recording in android using cordova media plugin

1k Views Asked by At

I'm using angularjs in visual studio.using cordova media plugin startRecord() and stopRecord() is working but not able to pause and resume recording.I'm not using media capture plugin as i don't have default recorder installed.

This is my code:

var audurl = '///storage/emulated/0/New/';
audurl += 'Voice_' + '.amr';
var mediaRec;
function recordAudio() {
  mediaRec = new Media(audurl, onSuccess, onError);
  mediaRec.startRecord();
}
function pauseAudio() {
  mediaRec = new Media(audurl, onSuccess, onError);
  mediaRec.pauseRecord();
}

thanks...

1

There are 1 best solutions below

15
On BEST ANSWER

On my phone the method media.resumeRecord was not available, although in this soure-code it is defined. Nevertheless you can take advantage of all other methods, like startRecord and stopRecord, to rebuild a kind of resumeRecord function, as it is done in a handler below:

var myRecordHandler = function () {

  // ALL RECORDED FILES ARE SAVED IN THIS ARRAY
  var recordedAudioFiles = [];

  // REMEMBER POSITION WHEN PLAYING IS STOPPED
  var currentPosition = {index:0,shift:0};

  // PAUSE-MODE
  var paused = false;

  // SET A SPECIFC DIRECTORY WHERE THE FILES ARE STORED INTO
  // DEFAULT: ''
  this.setDirectory = function(dir) {this.dir=dir;};

  // SET FILENAME
  // DEFAULT: recoredFilesX
  this.setFilename = function(filename) {this.filename=filename;};

  // SET MIME/Type of THE FILES; 
  // DEFAULT: mp3
  this.setFileType = function(type) {this.filetype=type;};

  // GET ALL RECORED FILES 
  this.getAllFiles = function() {return recordedAudioFiles;};

  // STOP/PAUSE RECORDED FILES
  var handleRecordedFileHold = function () {
    for (var r = 0; r < recordedAudioFiles.length; r++) {

      var recordedAudioFile = recordedAudioFiles[r];

      if(recordedAudioFile.isBeingRecorded){
         if(paused)recordedAudioFile.media.pause();
         else recordedAudioFile.media.stop();
         continue;
      }

      recordedAudioFile.duration = new Date().getTime() - recordedAudioFile.startTime;
      // call release to free this created file so that it could get deleted for instance
      recordedAudioFile.media.stopRecord();
      recordedAudioFile.media.release();
      recordedAudioFile.isBeingRecorded = true;
    }
  } 

  // START RECORDING
  this.startAudioRecording = function() {

    paused = false;
    handleRecordedFileHold();

    var dir = this.dir ? this.dir : '';
    var filename = this.filename ? this.filename : 'recoredFiles';
    var type = this.filetype ? this.filetype : 'mp3';

    var src = dir + filename + (recordedAudioFiles.length + 1) + '.' + type;
    var mediaRec = new Media(src, 
       function () {
         console.log('recordAudio():Audio Success');
       }, 
       function (err) {
         console.log('recordAudio():Audio Error: ' + err.code);
    });
    recordedAudioFiles.push({
      media: mediaRec,
      startTime: new Date().getTime()
    });
    mediaRec.startRecord();
  } 

  // PAUSE RECORDING
  this.pauseRecoredFiles = function () {
    if(recordedAudioFiles.length){
       paused = true;
       clearTimeout(currentPosition.timeout);
       handleRecordedFileHold();
       var recoredMedia = recordedAudioFiles[currentPosition.index].media;
       recoredMedia.getCurrentPosition(
           function (position) {
              currentPosition.shift = position;
           },
           function (e) {
              console.log("Error getting pos=" + e);
           }
       );
    }
  } 

  // PLAY RECORD
  this.playRecordedFiles = function () {
    handleRecordedFileHold();
    var playNextFile = function () {
      var recoredMedia = recordedAudioFiles[currentPosition.index];
      if (recoredMedia) {
        if(paused){
          recoredMedia.media.seekTo(currentPosition.shift*1000);
          paused = false;
        }
        recoredMedia.media.play();
        currentPosition.timeout = setTimeout(function () {
          currentPosition.index++;
          recoredMedia.media.stop();
          playNextFile();
        }, recoredMedia.duration ? recoredMedia.duration : 0);
      }
      else{
        paused = false;
        currentPosition.index = currentPosition.shift = 0;
      }
    };
    playNextFile();
  } 

  // RESET PLAY
  this.stopRecordedFiles = function () {
    currentPosition.index = currentPosition.shift = 0;
    clearTimeout(currentPosition.timeout);
    handleRecordedFileHold();
  }

  // REMOVE ALL RECORDED FILES 
  this.removeRecordedFiles = function() {
    paused = false;
    currentPosition.index = currentPosition.shift = 0;
    clearTimeout(currentPosition.timeout);
    handleRecordedFileHold();
    recordedAudioFiles = [];
  }

};

var handler = new myRecordHandler();

// you can use this handler in your functions like this:
function recordAudio() {
  // records one track and stops former track if there is one
  handler.startAudioRecording();
}

function playAudio() {
  handler.playRecordedFiles();
}

function pauseAudio() {
  handler.pauseRecoredFiles();
}

function resumeAudio() {
  pauseAudio();
  recordAudio();
}

function stopAudio() {
  handler.stopRecordedFiles();
}

Although I could not test your directory/filenames, because I do not have this directory created on my phone, these methods might help you to to store your files in a specific directory as well as with certain filenames:

handler.setDirectory('__YOUR_DIR__');

handler.setFilename('__YOUR_FILENAME__');

handler.setFileType('__YOUR_FILETYPE__');