2012-09-20 59 views
1

我需要知道在上傳到服務器之前通過PhoneGap捕獲的視頻的持續時間。 MediaFile.getFormatData的文檔有點模糊,似乎沒有工作。如何獲得通過PhoneGap捕獲的視頻的持續時間?

我已經能夠成功地捕獲和上傳視頻,所以所有這些工作,只是持續時間部分不是。

我在做什麼錯?

運行在iPhone 4的iOS 5.1.1

navigator.device.capture.captureVideo(function(mediaFile) { 
     if(mediaFiles.length == 1) { 
      $('#video_url').val(mediaFiles[0]); 
      var profileVideo = document.getElementById('profile-video'); 
      profileVideo.src = mediaFiles[0].fullPath; 

      var formatData; 
      mediaFiles[0].getFormatData(function(data) { 
       formatData = data; 
      }); 

      if(formatData.duration > 30) { 
       $('#infoMessage').html("Your video is longer than the allowed 30 seconds. Please record a new video. You can trim your video after it's been recorded.") 
      } 
     } 
    }, function(error) { 
    }, null); 

回答

2

您正在試圖調用異步代碼以同步的方式。嘗試像這樣代替:

mediaFiles[0].getFormatData(function(data) { 
    if(data.duration > 30) { 
     $('#infoMessage').html("Your video is longer than the allowed 30 seconds. Please record a new video. You can trim your video after it's been recorded.") 
    } 
}); 
+0

感謝西蒙......我想你的代碼,它仍然沒有工作:(我把警報只是如果(數據之前.duration> 30)來顯示data.duration值和警報永不會出現 – SomethingOn

+0

這應該工作,但我剛剛注意到在你的原始示例中的一件事是,你傳入「mediaFile」,但將它稱爲「mediaFiles」 –

+0

感謝Simon,我會看看......一個爲期一週的App Store發佈準備工作:D – SomethingOn

1

試試這個:

navigator.device.capture.captureVideo(function(mediaFiles) { 
      mediaFiles[0].getFormatData(function(data) { 
       if(data.duration > 30) { 
        alert('Your video is longer than the allowed 30 seconds.'); 
       } 
      }); 
    }, function(error) { alert('An error occured'); }, null); 
+0

對不起。我沒有看到我的答案看起來像西蒙的。 –

相關問題