我正在使用以下代码在 Android 设备上使用 Cordova Media 插件录制音频。尽管在 stopRecord() 上成功返回,但这会导致一个空的音频文件。有人可以指出这段代码可能有什么问题吗?
$cordovaFile.createFile(cordova.file.dataDirectory, 'new-rec.amr'), false)
.then(function (fileObj) {
console.log('File created', fileObj);
var nativePath = fileObj.nativeURL;
resolveLocalFileSystemURL(nativePath, function (entry) {
var internalPath = entry.toInternalURL();
var mediaRec = new Media(internalPath,
//success callback
function (success) {
console.log("record success", success);
},
//error callback
function (err) {
console.log("record error: " + JSON.stringify(err));
});
// Start recording audio
mediaRec.startRecord();
});
}, function (error) {
console.log('Cannot create a file to initiate a new recording', error);
});
最佳答案
我今天遇到了同样的问题。通过释放 Media 对象来解决它。我不确定为什么这会有所帮助,但我有一种感觉,操作系统可能会保留媒体对象而不是保存它,然后一旦它被释放,它就会被正确保存?
虽然不是 100% 确定。它无需在其他 Cordova 目录(例如 cordova.file.externalDataDirectory)中发布即可工作。
这是我的代码:
var fileName = randomFileName();
fileName = fileName + ".aac";
var store = cordova.file.dataDirectory;
window.resolveLocalFileSystemURL(store, function(dir){
var directoryToSave = dir.nativeURL + fileName;
$scope.audioRecording = new Media(directoryToSave, audioRecordingSuccess, audioRecordingFail, audioStatus);
$scope.audioRecording.startRecord();
setTimeout(function(){
$scope.audioRecording.stopRecord();
$scope.audioRecording.release();
}, 5000);
});
关于android - 使用 cordova 媒体插件录制音频会生成空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34604451/