web-dev-qa-db-ja.com

Android Webviewアプリでは、ビデオプレーヤーを全画面表示できません

こんにちは、ビデオサイト用に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" />

(Manifest.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" />
19
digiboomz

WebChromeClientのshowCustomViewhideCustomViewメソッドを実装する必要があります。また、Android:hardwareAccelerated="true"あなたのAndroidManifestファイルに。サンプルプロジェクトをここに投稿しています。私がしたことは、main.xmlに1つのFramelayout(customContainer)を保持し、ここでshowCustomViewで受け取ったビューを追加し、onHideでそれを削除することです。また、それに応じてウェブビューを非表示/表示します。以下のコードはデバイス上で完全に機能します。

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.
        }
    }

}

ここからサンプルプロジェクトを複製できます。

59
Akhil