【问题标题】:Android Webview app won't let video player go full screenAndroid Webview 应用程序不会让视频播放器全屏显示
【发布时间】:2013-03-25 16:02:59
【问题描述】:

您好,我为我的视频网站创建了一个 WebView 应用。该网站的设计是为移动用户加载的混合体。只有与移动设备兼容的视频才会加载到混合设备上。播放器来自 Vk、DailyMotion、YouTube 和 QuickTime。

视频只能在 SDK 11 及更高版本上播放,但是当我单击播放器按钮进入全屏模式时,它只会停止播放视频,而不会启动到全屏模式。

(Webviewactivity.java)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);

    parentView = (RelativeLayout) findViewById(R.id.parent_rl);

    webviewProgress = (ProgressBar) findViewById(R.id.webview_progress);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.setWebViewClient(new MyWebViewClient());
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    webview.loadUrl(URL);
    webviewProgress.setProgress(0);

    webview.setWebChromeClient(new MyWebChromeClient());
    webview.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            mProgressDialog = new ProgressDialog(WebViewActivity.this);
            mProgressDialog.setMessage("Downloading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setMax(100);
            mProgressDialog
                    .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            DownloadFile downloadFile = new DownloadFile();
            downloadFile.execute(url);
        }
    });

    initSlider();

    initAdmob();
}

/**
 * When when file was chosen
 */
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage)
            return;
        Uri result = intent == null || resultCode != RESULT_OK ? null
                : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;

(Main.xml)

android:id="@+id/parent_rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >

<ProgressBar
    android:id="@+id/webview_progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:maxHeight="5dip"
    android:minHeight="5dip"
    android:progressDrawable="@drawable/blueprogress" />

<FrameLayout
    android:id="@+id/framelayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/webview_progress"
    android:orientation="vertical" >

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

(清单.xml)

package="com.wCHfree"
android:versionCode="7"
android:versionName="1.1" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:icon="@drawable/ic_launcher_red"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black" >
    <activity
        android:name="com.webview.splashScreen.SplashScreenActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.webview.splashScreen.WebViewActivity"
        android:configChanges="orientation|screenSize|screenLayout"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    </activity>
    <activity
        android:name="com.google.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

【问题讨论】:

    标签: android eclipse webview video-player


    【解决方案1】:

    你需要实现 WebChromeClient 的showCustomView & hideCustomView 方法,你还需要在你的AndroidManifest 文件中使用android:hardwareAccelerated="true"。 我在这里发布我的示例项目。 我所做的是在我的 main.xml 中保留一个 Framelayout(customContainer),并在此处添加在 showCustomView 中收到的视图,并在 onHide 中将其删除。也相应地隐藏/显示 webview。下面的代码在设备上完美运行。

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.webview"
              android:versionCode="1"
              android:versionName="1.0">
        <uses-sdk android:minSdkVersion="8"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"
                android:hardwareAccelerated="true">
            <activity android:name="MyActivity"
                      android:configChanges="orientation|keyboardHidden"
                      android:hardwareAccelerated="true"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    </manifest>
    

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
            >
        <WebView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/webView"
                android:layout_gravity="center"
                />
        <FrameLayout
                android:id="@+id/customViewContainer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:visibility="gone"
                />
    </LinearLayout>
    

    video_progress.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/progress_indicator"
                  android:orientation="vertical"
                  android:layout_centerInParent="true"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
    
        <ProgressBar android:id="@android:id/progress"
                     style="?android:attr/progressBarStyleLarge"
                     android:layout_gravity="center"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"/>
    
        <TextView android:paddingTop="5dip"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:text="loading"
                  android:textSize="14sp"
                  android:textColor="?android:attr/textColorPrimary"/>
    </LinearLayout>
    

    MyActivity.java

    package com.example.webview;
    
    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.
            }
        }
    
    }
    

    You can clone sample project from here..

    【讨论】:

    • 谢谢,我试试看。
    • 也谢谢你,这个回答很权威。我会试一试,如果一切顺利 - 赏金就是你的了。
    • 我必须说这个示例项目比我自己的更好。我有在 2.3.3 上工作的视频,这些视频不适用于我自己的项目的那个版本。甚至不知道你是怎么做到的。
    • 位图有什么用?此代码中的其他任何地方都没有引用它
    • @Stan 我在onCreateView 中获取指向 webview mWebView = (WebView) v.findViewById(R.id.webView); 的链接没有问题。这是另一个问题。无论如何,我尝试转换另一个示例代码以在片段inducesmile.com/android/… 中使用它,它运行良好,但我无法使用onBackPressed 方法(因为片段不会覆盖它)。所以无法退出全屏
    猜你喜欢
    • 2013-12-11
    • 2013-03-24
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2014-10-24
    相关资源
    最近更新 更多