2014-09-30 64 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); 
     }); 
相關問題