Android embed video in iframe not resized on webview height change

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I am trying to play embeded youtube videos in webview. My problem is when I rotate screen and resize webview height, iframe is not changing its height.

String embedSrc = "https://www.youtube.com/embed/8SeRU_ZPDkE"; String iframe = "<html><body style=\"margin: 0; padding: 0; background: #000;\"><iframe width=\"100%\" height=\" 100% \" src=\"" + embedSrc + "frameborder=\"0\" allowfullscreen style=\"background: #000;\"></iframe></body></html>";  webView.loadData(iframe, "text/html", "utf-8");   WebSettings settings = webView.getSettings();         settings.setJavaScriptEnabled(true);         settings.setBuiltInZoomControls(false);         settings.setDomStorageEnabled(true);          String userAgent = settings.getUserAgentString();         if (userAgent != null) {             userAgent = userAgent.replace("Android", "");             settings.setUserAgentString(userAgent);         }  //code to update webview height webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, newHeight));   //layout <LinearLayout     android:id="@+id/root_view"     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#000"     android:orientation="vertical"     >      <WebView         android:id="@+id/webview"         android:layout_width="wrap_content"         android:layout_height="250dp"         android:scrollbars="none"         />  </LinearLayout> 

I already tried this

webview.getSettings().setUseWideViewPort(true); webview.getSettings().setLoadWithOverviewMode(true); 

But this code makes youtube Playback controls very small.

回答1:

Solved this problem with iframe in webview by using this Responsive Iframe CSS tip like this:

<style> .video-container {  position: relative;  padding-bottom: 56.25%;  padding-top: 35px;  height: 0;  overflow: hidden;  } .video-container iframe {  position: absolute;  top:0;  left: 0;  width: 100%;  height: 100%;  } </style> <div class="video-container">     <iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" allowfullscreen="" frameborder="0">     </iframe> </div> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!