2016-07-08 97 views
1

我正在編寫一個應用程序,它將接收音頻文件,然後將其上傳到Teleriks永久服務。我想阻止用戶上傳長度超過20秒的任何音頻文件。Cordova Media Capture - 查找音頻文件的持續時間

但經過測試,似乎無論音頻文件的持續時間,「data.duration」似乎總是返回0,所以無論文件上傳了什麼。

這是我使用的插件:https://github.com/apache/cordova-plugin-media-capture

這是我的代碼:

var el = new Everlive('app-id-here'); //Taken away ID for stackoverflow 

    var options = { 
     fileName: 'testaudio.mp3', 
     mimeType: 'audio/mpeg3' 
    }; 

    function formatSuccess(data) { 
     if (data.duration > 20) { 
      alert("The audio file you have used is longer than 20 seconds."); 
     } else { 
      alert(data.duration); 
      el.files.upload(capturedFiles[0].fullPath, options) 
       .then(function() { 
        alert('success'); 
       }, function (err) { 
        alert(JSON.stringify(err)); 
       }); 
     } 

    } 

    function formatFail() { 
     alert("Error accessing file data"); 

    } 

    capturedFiles[0].getFormatData(formatSuccess, formatFail); 

回答

0

上次我這個玩,getDuration把媒體文件播放時只工作。從我的Apache Cordova API食譜(www.cordovacookbook.com):

應用程序可以確定爲顯示在下面的示例使用getDuration一個媒體文件的長度:

console.log('Duration: ' + theMedia.getDuration() + ' seconds'); 

getDuration將報告-1如果音頻剪輯當前沒有播放。與getCurrentPosition不同,即使播放暫停,getDuration方法也會返回片段長度。

+0

你有沒有找到這方面的解決方案?我也得到-1持續時間。 – Mikethetechy

相關問題