1

In my application I need to make sure that a user has set a video as source of my html5 video player, before continuing with the other actions. So I tried this (using Firebug in Firefox):

When user has set a source for the videoplayer

vp = $('#myvideoplayer')
vp.attr('src')  --> "blob:5c254454-6c4e-4b69-b381-9ad60d6b1c4a"

when no source is set for the video

vp.attr('src')  --> undefined

Can I use this to detect if a user has set the video source or not?

if(vp.attr('src') != undefined){
   let_user_do_next_action();
}

Or is there any better way?

p.s:

I am setting the source of video using an HTML5 file input widget as follows. (This works only in Firefox, since Chrome doesn't allow createObjectURL from window.webkitURL for some reason.)

var file = $('#fileselect').get(0).files[0];
if (file==undefined){
    alert('no file chosen');
}
var myURL = window.URL || window.webkitURL;
var fileURL = myURL.createObjectURL(file);
if(fileURL){
    player = $('#myvideoplayer');
    player.src=fileURL;
    player.load();
}
return;
4

1 に答える 1

1

そのコードは機能するはずです。または、確実になりたい場合は、チェックしてくださいtypeof vp.attr('src') == "undefined"。ただし、jQueryを使用しない場合は、element.hasAttribute("src")代わりにチェックすることをお勧めします。

についてwindow.webkitURL.createObjectURL、Chromeはローカルで(たとえばURLで)テストするときに制限があると思います。file://ローカルホストで単純なHTTPサーバーをセットアップするか、Chromeをで起動する必要があり--allow-file-access-from-filesます。

于 2012-05-28T04:15:10.297 に答える