2014-09-30 65 views
1

我现在的应用程序非常简单。它会记录一个音频文件。我想要在phonegap API中使用readAsDataURL()
将音频文件转换为base64字符串。以下是我的代码如下。正如你所看到的,我遵循documentation获取下面的base64字符串。在下面的代码中,我试图获取base64字符串并将其存储在变量中,然后使用该变量在当前的警告框中显示该字符串。截至目前,我只能获得“数据:音频/波形; base64”,现在我已经坚持了4周。我真的很感谢一些帮助。如何使用Phonegap将音频文件转换为iOS上的base64字符串?

function gotFS(fileSystem) { 
    fileSystem.root.getFile("myaudio.wav", {create: true, exclusive: false}, gotFileEntry, fail); 
} 

function gotFileEntry(fileEntry) { 
    fileEntry.file(gotFile, fail); 

} 

function gotFile(file){ 
    readDataUrl(file); 

} 

function readDataUrl(file) { 
var reader = new FileReader(); 
reader.onloadend = function(evt) { 
    console.log("read success"); 
    console.log(evt.target.result); 
    var thed = evt.target.result; 
    alert(thed); //trying to display the base64 string in an alert box   
    }; 
    reader.readAsDataURL(file); 
} 

回答

0
document.addEventListener("deviceready", init, false); 
    function init() { 


     //This alias is a read-only pointer to the app itself 
     window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/button-1.mp3", gotFile, fail); 

     // var src = "/android_asset/www/sounds/button-1.mp3"; 

    } 

    function fail(e) { 
     // console.log("FileSystem Error"); 
     // console.dir(e); 
    } 

    function gotFile(fileEntry) { 

     fileEntry.file(function(file) { 
     var reader = new FileReader(); 

     reader.onloadend = function(e) { 
      console.log("Text is: "+this.result); 

     }; 

     // reader.readAsText(file); 
     // reader.readAsArrayBuffer(file); 
     // reader.readAsBinaryString(file); 
     reader.readAsDataURL(file); 
     }); 
相关问题