我使用以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_web_view_play_youtube);
WebView w = (WebView) findViewById(R.id.w);
w.setWebChromeClient(new WebChromeClient());
w.setWebViewClient(new WebViewClient());
w.getSettings().setJavaScriptEnabled(true);
w.loadUrl("https://www.youtube.com/watch?v=gY-HZg1Uwpc");
}
我得到以下截图
在此屏幕截图中,我看不到" fullScreen"按钮。
答案 0 :(得分:6)
在您的情况下,您必须先在XML文件中添加FrameLayout
。之后
您必须实施onShowCustomView
onHideCustomView
和WebChromeClient
两种方法,如下所示:
FrameLayout customViewContainer = findViewById(R.id.customViewContainer);
webView.setWebChromeClient(new WebChromeClient() {
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
super.onShowCustomView(view,callback);
webView.setVisibility(View.GONE);
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.addView(view);
}
public void onHideCustomView () {
super.onHideCustomView();
webView.setVisibility(View.VISIBLE);
customViewContainer.setVisibility(View.GONE);
}
});
答案 1 :(得分:0)
我应该自定义WebChromeClient#onShowCustomView和#onHideCustomView,下面的代码将显示全屏按钮:
public class TestWebViewPlayYoutube extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_web_view_play_youtube);
WebView w = (WebView) findViewById(R.id.w);
w.setWebChromeClient(new CrmClient());
w.setWebViewClient(new WebViewClient());
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setMediaPlaybackRequiresUserGesture(false);
w.loadUrl("https://www.youtube.com/watch?v=gY-HZg1Uwpc&autoplay=1");
}
class CrmClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
}
}
}
<强>更新强>
我改进了我的代码,它将显示全屏并隐藏全屏html5(对于ex youtube视频播放器),使用此代码,您必须确保main / assets / test.mp4存在 < / p>
public class TestFullscreen2 extends Activity {
WebView w;
RelativeLayout container;
float dp;
static FrameLayout fullscreenV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_fullscreen2);
dp = getDp(this);
container = (RelativeLayout) findViewById(R.id.container);
w = (WebView) findViewById(R.id.w);
if (fullscreenV != null && fullscreenV.getParent() == null) {
w.setVisibility(GONE);
container.addView(fullscreenV);
}
w.setWebChromeClient(new CrmClient(this));
w.setWebViewClient(new WebViewClient());
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setMediaPlaybackRequiresUserGesture(false);
w.loadDataWithBaseURL("file:///android_asset/", "<video width='100%' height='auto' src='test.mp4' controls autoplay/>", "text/html", "utf-8", null);
}
@Override
protected void onDestroy() {
super.onDestroy();
w = null;
}
class CrmClient extends WebChromeClient {
Activity a;
public CrmClient(Activity a) {
this.a = a;
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
fullscreenV = (FrameLayout) view;
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
fullscreenV = null;
a.startActivity(new Intent(a, TestFullscreen2.class) {{
setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}});
}
}
}