tôi đã được học một số javascript
, để chọn một tập tin và sử dụng nó để tạo ra một objectUrl có thể được thiết lập như là src
của một html5 video
.Tôi đang cố gắng này ra trong chrome phiên bản 18.0 .1025.162 trên ubuntu lucid
và sử dụng tệp html cục bộ có tệp javascript và tệp phương tiện trong cùng một thư mục.javascript window.URL là undefined chức năng
Tôi có thể chọn tệp video bằng yếu tố đầu vào và khi tôi nhấp vào play link
, chức năng javascript playVideo() được thực thi.
<video id="vid" name='myvid' width="640" height="360" controls="controls">
<source src="test.webm" type="video/webm" />
</video>
<br><a href="#" id="playlnk">Play </a> </li>
<br><input type="file" name="fileselect" id="fselect"> </input>
javascript tập tin là
$(document).ready(function(){
player=$('#vid').get(0);
$('#playlink').click(function(){playVideo(player);});
});
function setVideoSrc(player,file){
console.log('winurl='+window.URL);
var fileURL = window.URL.createObjectURL(file);
player.src=fileURL;
player.load();
return;
}
function playVideo(player) {
var file=document.getElementById('fselect').files[0];
console.log('you chose:'+file);
if (file==undefined){
console.log('no file chosen');
}else{
console.log('file='+file);
setVideoSrc(player,file);
}
}
Khi tôi không chọn bất kỳ tập tin và nhấn vào playlink, phát video mặc định và giao diện điều khiển log nói no file chosen
-Như mong đợi.
Lỗi xảy ra khi Tôi chọn tệp video và sau đó nhấp vào liên kết phát sóng. Sau đó, phương thức playVideo()
gọi setVideoSrc()
trong đó nhật ký bảng điều khiển cho biết window.URL' is
undefined`
Tại sao điều này xảy ra? Ai đó có thể giúp tôi sửa lỗi này? Đây là giao diện điều khiển dữ liệu ghi nhận
you chose:[object File] //from playVideo() function
file=[object File] //from playVideo() function
winurl=undefined //setVideoSrc() function
Uncaught TypeError: Cannot call method 'createObjectURL' of undefined
[window.URL] (https : //developer.mozilla.org/en/DOM/window.URL) là Gecko cụ thể. Trình duyệt mà bạn đang sử dụng? –
Tôi đang sử dụng ..chrome, có cách nào độc lập với trình duyệt để thực hiện việc này không? – damon
Theo hiểu biết của tôi, không có (IE và Opera dường như không hỗ trợ điều này). Tuy nhiên, Chrome có tương đương ('window.webkitURL') [theo MDN] (https://developer.mozilla.org/en/DOM/window.URL.createObjectURL). –