繁体   English   中英

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

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

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

这是我的代码:

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

谢谢...

在我的手机上, media.resumeRecord方法不可用,尽管在这个源代码中它是定义的。 不过,您可以利用所有其他方法(如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__');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM