如何使用cordova媒体插件在Android中暂停和恢复录音

时间:2017-02-12 04:11:39

标签: javascript cordova cordova-plugins audio-recording cordova-media-plugin

我在visual studio中使用angularjs。使用cordova媒体插件startRecord()和stopRecord()正在工作但无法暂停和恢复录制。我没有使用媒体捕获插件,因为我不喜欢&# 39;安装了默认录像机。

这是我的代码:

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();
}

感谢...

1 个答案:

答案 0 :(得分:0)

在我的手机上,方法 media.resumeRecord 不可用,但在此soure-code中已定义。不过,您可以利用所有其他方法(如startRecordstopRecord)来重建一种resumeRecord函数,就像在下面的处理程序中完成一样:

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();
}

虽然我无法测试您的目录/文件名,因为我没有在手机上创建此目录,这些方法可能会帮助您将文件存储在特定目录中以及某些文件名:

handler.setDirectory('__YOUR_DIR__');

handler.setFilename('__YOUR_FILENAME__');

handler.setFileType('__YOUR_FILETYPE__');