在android webview中制作嵌入视频全屏

Posted

技术标签:

【中文标题】在android webview中制作嵌入视频全屏【英文标题】:Making an embed video full screen in android webview 【发布时间】:2015-03-27 23:55:39 【问题描述】:

我想从网站获取嵌入视频,然后像默认的 android 浏览器一样全屏播放。我一直在寻找解决方案,但我做不到。我的视频播放正常,但我希望它弹出并全屏显示。任何建议将不胜感激。

【问题讨论】:

Android Webview app won't let video player go full screen的可能重复 【参考方案1】:

尝试让它在全屏模式下工作对我来说很麻烦,直到我在 github 上发现 this project 就像一个魅力一样工作。

这是它的副本。

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MyActivity extends Activity 
    private WebView webView;
    private FrameLayout customViewContainer;
    private WebChromeClient.CustomViewCallback customViewCallback;
    private View mCustomView;
    private myWebChromeClient mWebChromeClient;
    private myWebViewClient mWebViewClient;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer);
        webView = (WebView) findViewById(R.id.webView);

        mWebViewClient = new myWebViewClient();
        webView.setWebViewClient(mWebViewClient);

        mWebChromeClient = new myWebChromeClient();
        webView.setWebChromeClient(mWebChromeClient);
        webView.getSettings().setjavascriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSaveFormData(true);
        webView.loadUrl("http://m.youtube.com");
    

    public boolean inCustomView() 
        return (mCustomView != null);
    

    public void hideCustomView() 
        mWebChromeClient.onHideCustomView();
    

    @Override
    protected void onPause() 
        super.onPause();    //To change body of overridden methods use File | Settings | File Templates.
        webView.onPause();
    

    @Override
    protected void onResume() 
        super.onResume();    //To change body of overridden methods use File | Settings | File Templates.
        webView.onResume();
    

    @Override
    protected void onStop() 
        super.onStop();    //To change body of overridden methods use File | Settings | File Templates.
        if (inCustomView()) 
            hideCustomView();
        
    

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
        if (keyCode == KeyEvent.KEYCODE_BACK) 

            if (inCustomView()) 
                hideCustomView();
                return true;
            

            if ((mCustomView == null) && webView.canGoBack()) 
                webView.goBack();
                return true;
            
        
        return super.onKeyDown(keyCode, event);
    

    class myWebChromeClient extends WebChromeClient 
        private Bitmap mDefaultVideoPoster;
        private View mVideoProgressView;

        @Override
        public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) 
           onShowCustomView(view, callback);    //To change body of overridden methods use File | Settings | File Templates.
        

        @Override
        public void onShowCustomView(View view,CustomViewCallback callback) 

            // if a view already exists then immediately terminate the new one
            if (mCustomView != null) 
                callback.onCustomViewHidden();
                return;
            
            mCustomView = view;
            webView.setVisibility(View.GONE);
            customViewContainer.setVisibility(View.VISIBLE);
            customViewContainer.addView(view);
            customViewCallback = callback;
        

        @Override
        public View getVideoLoadingProgressView() 

            if (mVideoProgressView == null) 
                LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
                mVideoProgressView = inflater.inflate(R.layout.video_progress, null);
            
            return mVideoProgressView;
        

        @Override
        public void onHideCustomView() 
            super.onHideCustomView();    //To change body of overridden methods use File | Settings | File Templates.
            if (mCustomView == null)
                return;

            webView.setVisibility(View.VISIBLE);
            customViewContainer.setVisibility(View.GONE);

            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);

            // Remove the custom view from its container.
            customViewContainer.removeView(mCustomView);
            customViewCallback.onCustomViewHidden();

            mCustomView = null;
        
    

    class myWebViewClient extends WebViewClient 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
            return super.shouldOverrideUrlLoading(view, url);    //To change body of overridden methods use File | Settings | File Templates.
        
    


【讨论】:

webView.setWebViewClient(mWebViewClient);此行显示为空,有什么提示吗? webView = (WebView) findViewById(R.id.webView);检查id是否正确

以上是关于在android webview中制作嵌入视频全屏的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌入式 youtube 视频的 Android WebView,全屏按钮冻结视频

Android WebView html5 视频强制全屏

如何在 Android WebView 中以横向全屏播放视频?

android 6中的android webview youtube视频中的全屏模式问题

Android WebView全屏视频退出创建额外的空白区域

关闭全屏视图后的Android WebView Video,webview自动滚动