1

我在visual studio中使用angularjs。使用cordova media插件startRecord()和stopRecord()正在工作,但无法暂停和恢复录制。我是不使用媒体捕获插件,因为我没有安装默认记录器。如何在android中使用cordova media插件暂停和恢复录音

这是我的代码:

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

感谢...

+0

[这](HTTPS:/ /github.c om/apache/cordova-plugin-media#quick-example-7)示例显示了你想知道的一切。虽然它会自动暂停和恢复(通过使用setTimeout),但它会显示如何恢复录制。 – Blauharley

+0

我可以暂停并继续点击按钮。 – abc

+0

我认为按照文档暂停,仅在iOS中恢复工作。 – abc

回答

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__'); 
+0

也尝试过这样但同样的问题。如果每次都生成ID,stopRecord()如何工作。 – abc

+0

您是否已经播放过录制的音频文件?录制过程中是否有红色LED灯闪烁?这些方法应该工作,否则你必须通知开发者自己,对不起。 – Blauharley

+0

是的,但没有led灯发光。其实我没有使用任何默认记录器,我已经创建了一个用户界面来做到这一点 – abc