2014-01-08 60 views
1

我試圖通過使用phonegap的File API上傳捕獲的音頻文件。我將音頻文件錄製爲mp3文件,然後在文件系統中找到該文件,然後將文件上傳到服務器。該文件的記錄工作正常,我可以在我的本地存儲中播放它。該文件的上傳也似乎工作正常,但我無法播放上傳的MP3文件。所以我問什麼?是我嘗試上傳的錄製文件的路徑是否正確,並且實際上是否從媒體文件中獲得了正確的base64編碼字符串,或者是由於我的設備無法找到我正在尋找的文件而創建新文件?目前我正在使用Android,但如果它是iOS的怪癖,我也很想知道它們。當我創建mp3文件時,它存儲在my/storage/emulated/0目錄中。當我在Firefox中打開mp3文件時,mp3文件的持續時間僅爲1秒,應該是7秒。可能是什麼問題呢?我非常感謝能夠獲得的每一個幫助,你可以在下面看到我的代碼。上傳帶phonegap的媒體文件,Android

// The src of the mp3 file, works when I play it after in my local storage 
    src = "recording.mp3"; 
     mediaRec = new Media(src, onSuccess, onError); 

     // Record audio 
     mediaRec.startRecord(); 

     // Stop recording after 7 sec 
       mediaRec.stopRecord(); 

// get the file from the file system (local storage) 
       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
// Get the file from the file system 

function gotFS(fileSystem) { 
     fileSystem.root.getFile("recording.mp3", null, gotFileEntry, fail); 
} 

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

// the file is successfully retreived 

function gotFile(file){ 
    readDataUrl(file); 
} 

// turn the file into a base64 encoded string, and update the var base to this value. 

function readDataUrl(file) { 
    var reader = new FileReader(); 
    reader.onloadend = function(evt) { 
    alert(evt.target.result); 
    base = evt.target.result; 
    }; 
    reader.readAsDataURL(file); 
} 

// later when this is done I upload the file to something called parse. This seems to work fine, 

var file = new Parse.File("sound.mp3", { base64: base }); file.save().then(function() { 
     Object.set("soundy", file); 
    }, function(error) { 
     alert("an error"); 
    // The file either could not be read, or could not be saved to Parse. 
    }); 

該文件現在已成功保存爲解析對象中的mp3文件,但我無法播放它。我真的很感謝我能得到的每一個幫助

+0

大問題,將您uload代碼的回調函數裏面readDataUrl()!!!!!!!我也有同樣的問題!!!!!!當我發現它時會發佈一個答案,如果它不是先來這裏的話!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! – Femtosecond

回答

0

也許你的問題是,當你嘗試上傳文件時,文件讀取是同步的並且文件沒有被完全讀取?

你可以嘗試(剛過base = evt.target.result;線)

+0

我會盡力做到這一點,或使其同步。 reader.readAsDataURL(文件)到底做了什麼? –

+0

情況並非如此。我啓用了在readDataURL()之後上傳base64字符串的按鈕。聽錄製的音頻我仍然有問題:( –