在android中切换设备屏幕时,全屏视频自定义视图会退出

问题描述 投票:1回答:1

我正在开发一个完全基于网络的android应用程序。它需要使用具有丰富多媒体内容的webview显示基于HTML5的网页。当我尝试播放嵌入在网页中的视频内容时,一切正常,我也可以全屏播放视频。

但是我的主要问题从这里开始:当我在播放全屏视频的同时切换设备屏幕时,它突然从播放状态退出到当前网页。我被困在这里,这里包括所有我曾经使用过的代码,希望有人能帮助我。

使用的活动:MainActivity.java

`公共类MainActivity扩展了Activity {

private WebView webView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private MyWebViewClient mWebViewClient;
private MyWebChromeClient mWebChromeClient;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_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://yuotube.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);
}

// WebChromeClient Class  

public 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(MainActivity.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;
    }



}

// WebViewClient Class    
public 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.
    }
     ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "", 
             "Loading multimedia! Please wait...", true);

       @Override
       public void onPageFinished(WebView view, String url) {
        dialog.dismiss();
      }

    }

}

web_activity.xml

<!-- View that will be hidden when video goes fullscreen -->
<RelativeLayout
    android:id="@+id/nonVideoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <com.example.iptvmodified.VideoEnabledWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>   



<!-- View where the video will be shown when video goes fullscreen -->
<RelativeLayout
    android:id="@+id/videoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- View that will be shown while the fullscreen video loads (maybe include a spinner and a "Loading..." message) -->
    <View
        android:id="@+id/videoLoading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="invisible" />

</RelativeLayout>

`activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

我正在为此寻找解决方案,并将其用谷歌搜索搜索,没有取得任何进展。任何帮助将不胜感激。

android-webview html5-video
1个回答
0
投票

通常在旋转设备时,您的活动会被破坏并重新创建。这可能导致WebView被“踢出”全屏播放。

您需要重写Activity.onConfigurationChanged并声明要处理清单中的方向更改。有关详细信息,请参见docs

© www.soinside.com 2019 - 2024. All rights reserved.