2017-06-26 45 views
0

與科爾多瓦一起使用錄製應用程序並使用new Media進行錄製。我還使用了用於建立目錄cordoba-file-plugin等,所以,當應用程序初始化我跑科爾多瓦新媒體記錄保存到目錄

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs){ 
    app.root = fs; 
}, function(error){ 
    console.log(error); 
}); 

然後我運行此代碼:

app.root.root.getDirectory('appname', {create:true}, function(dirEntry){ 
    dirEntry.getDirectory('recordings', {create:true}, function(subDirEntry) { 
     app.recordings = subDirEntry; 
    }, function(error){ 
     console.log(error); 
    }); 
}, function(error){ 
    console.log(error); 
}); 

然後,我必須重新編碼和腳本我想直接將文件保存到app.recordings,這是我爲此應用程序創建的目錄。但沒有出現。

var path = app.recordings.nativeURL; 
var filename = path + "recording-" + new Date().getTime() + ".mp3"; 
var mediaRec = new Media(filename, app.services.audio.success, app.services.audio.error); 
mediaRec.startRecord(); 
mediaRec.stopRecord(); 

我一直收到一個tmprecording-12351834581925.3gp的根目錄。

回答

0

在設備準備

if(device.platform == "iOS"){ 
    app.path = cordova.file.tempDirectory; 
    app.extension = ".wav"; 
    app.mimeType = "audio/wav"; 
} else if(device.platform == "Android"){ 
    app.path = cordova.file.externalRootDirectory; 
    app.extension = ".amr"; 
    app.mimeType = "audio/amr"; 
} 

window.resolveLocalFileSystemURL(app.path, function(fileSystem){ 
    fileSystem.getDirectory("AppName", {create:true},function(dirEntry){ 
     dirEntry.getDirectory("Recording", {create:true} , function(subEntry){ 
      app.recording = subEntry.nativeURL; 
     }, function(error){ 
      console.log(error); 
     }); 
    }, function(error){ 
     console.log(error); 
    }); 
},function(error){ 
    console.log(error); 
}); 

然後在記錄

var filename = app.recording + new Date().getTime() + app.extension; 
var mediaRec = new Media(filename, success, error); 

這些文件現在保存在一個優選的位置,我現在有控制和訪問文件。在錄音

window.resolveLocalFileSystemURL(app.recording ,function (fileSystem) { 
    var reader = fileSystem.createReader(); 
    reader.readEntries(function (entries) { 
     console.log(entries); 
     //loop all entries 
    }, function(error){ 
     console.log(error); 
    }); 
} 

讀取文件