Reputation: 358
I'm embedding youtube live stream using webview in my android app. I'm using iframe
for the same. Is there a way i can set the size of video player according to screen size.
public class WebViewPlayer extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_player);
String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"400\" height=\"290\" src=\"https://www.youtube.com/embed/live_stream?channel=UCYn0pQcA8IMxk4cDFzlBF2w&autoplay=1\" frameborder=\"0\" allowfullscreen=\"true\"></iframe></body></html>";
WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadData(frameVideo, "text/html", "utf-8");
}
}
Upvotes: 1
Views: 1392
Reputation: 1738
You can use this player to play live YouTube videos in your Android app: android-youtube-player.
It is equivalent to using the IFrame player in a WebView, but nicer.
Here is the documentation about live videos.
Upvotes: 1
Reputation: 799
This is the iFrame that worked for me. You just have to pass the layout_width
of the webview
as match_parent
and a fix layout_height
. The iFrame contains a script as well you can remove that if it is not required. Let me know if this is helpful for you.
Iframe
"<html><body style='margin:0px;padding:0px;'><script type='text/javascript' " +
"src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>" +
"function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId'," +
"{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}"+
"</script>Youtube video .. <br><iframe id='playerId' type='text/html' width='100%' height='100%' " +
"https://www.youtube.com/embed/live_stream?channel=UCYn0pQcA8IMxk4cDFzlBF2w&autoplay=1' frameborder='0' allowfullscreen></body></html>"
webview.loadDataWithBaseURL(null, frameVideo, "text/html", "utf-8", null);
Upvotes: 2