javascript - youtube iframe player, video appears cropped in android -
i trying play youtube videos on android devices using webview , youtube iframe api. when tested on 4.4, 5.0, , 5.1. testing same on 4.3 video seems cropped. ie video occupies 70% of webview area. solutions ?!?!
my code follows..
java code
wvvideo = (webview)findviewbyid(r.id.wvvideo); wvvideo.setwebchromeclient(new webchromeclient()); websettings websettings = wvvideo.getsettings(); websettings.setjavascriptenabled(true); websettings.setmediaplaybackrequiresusergesture(false); wvvideo.addjavascriptinterface(new webappinterface(this), "android"); wvvideo.loadurl("http://test.com/iframeyoutube.html?videoid="+videourl); wvvideo.loadurl("javascript:playvideo();");
xml
<webview android:id="@+id/wvvideo" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerinparent="true" android:background="#212121"></webview>
html
<!doctype html> <html style="height:100%;margin:0;padding:0;width:100%"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="background:#212121;height:100%;margin:0;padding:0"> <!-- 1. <iframe> (and video player) replace <div> tag. --> <div id="player" style="margin:0;padding:0"></div> <script> var query = function () { var query_string = {}; var query = window.location.search.substring(1); var vars = query.split("&"); (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if (typeof query_string[pair[0]] === "undefined") { query_string[pair[0]] = pair[1]; } else if (typeof query_string[pair[0]] === "string") { var arr = [ query_string[pair[0]], pair[1] ]; query_string[pair[0]] = arr; } else { query_string[pair[0]].push(pair[1]); } } return query_string; } (); var tag = document.createelement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); var player; function onyoutubeiframeapiready() { player = new yt.player('player', { height:'100%',width:'100%', videoid: query.videoid, playervars: { 'autoplay': 0, 'controls': 0, 'showinfo':0, 'modestbranding':1}, events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); } function onplayerready(event) { cuecheck(); timerfunction(); videolength(); } var done = false; function onplayerstatechange(event) { android.playstate(event.data+""); if (event.data == yt.playerstate.playing && !done) { done = true; } } function cuecheck() { android.playstate("cued"); } function timerfunction(){ console.log("function"); android.updatetimer(player.getcurrenttime()+""); settimeout(timerfunction, 20); } function videolength(){ android.getvideolength(player.getduration()+""); } function seekto(val) { player.seekto(val); // player.playvideo(); } function pausevideo(){ player.pausevideo() } function playvideo(){ player.playvideo() } </script> </body> </html>
Comments
Post a Comment