Java 如何从webview观看全屏视频

Java 如何从webview观看全屏视频,java,android,webview,android-fullscreen,Java,Android,Webview,Android Fullscreen,这是我的密码。我为youtube构建了webview,并使用WebChromeClient尝试在应用程序上添加全屏功能。但当点击全屏时,它会出现一个空白屏幕,奥伊多在后台播放 维护活动 public class MainActivity extends AppCompatActivity { public WebView mywebview; @Override protected void onCreate(Bundle savedInstanceState) { super.onC

这是我的密码。我为youtube构建了webview,并使用WebChromeClient尝试在应用程序上添加全屏功能。但当点击全屏时,它会出现一个空白屏幕,奥伊多在后台播放

维护活动

public class MainActivity extends AppCompatActivity {

public WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mywebview = (WebView)findViewById(R.id.youtube_webview);

    WebSettings webSettings =mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.setWebChromeClient(new CrmClient());
    mywebview.setWebViewClient(new WebViewClient());
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setVerticalScrollBarEnabled(false);
    mywebview.setHorizontalScrollBarEnabled(false);
        mywebview.loadUrl("https://www.youtube.com");

}



class CrmClient extends WebChromeClient{

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();
    }
}




@Override
protected void onPause() {
    super.onPause();
    if(mywebview != null) {
        mywebview.onPause();
        mywebview.pauseTimers();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if(mywebview != null){
        mywebview.onResume();
        mywebview.resumeTimers();
    }
}

@Override
public void onBackPressed() {
    if (mywebview.canGoBack())
        mywebview.goBack();
    else
        super.onBackPressed();
}
活动\u main.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">

<WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/youtube_webview" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        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>

AndroidMainFest.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">

<WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/youtube_webview" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity"
        android:configChanges="orientation|screenSize"
        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>

>

试试这段代码

public class MainActivity extends AppCompatActivity {

public WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mywebview = (WebView)findViewById(R.id.youtube_webview);

    WebSettings webSettings =mywebview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mywebview.setWebViewClient(new WebViewClient());
    mywebview.getSettings().setJavaScriptEnabled(true);
    mywebview.setVerticalScrollBarEnabled(false);
    mywebview.setHorizontalScrollBarEnabled(false);
        mywebview.loadUrl("https://www.youtube.com");

 mywebview.setWebChromeClient(new WebChromeClient() {
            public void onShowCustomView(View view,
                                         WebChromeClient.CustomViewCallback callback) {

                if (mCustomView != null) {
                    onHideCustomView();
                    return;
                }

                mCustomView = view;
                mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
                mOriginalOrientation = getRequestedOrientation();

                mCustomViewCallback = callback;

                FrameLayout decor = (FrameLayout) getWindow().getDecorView();
                decor.addView(mCustomView, new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));


              getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                                View.SYSTEM_UI_FLAG_FULLSCREEN |
                                View.SYSTEM_UI_FLAG_IMMERSIVE);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }

            @Override
            public void onHideCustomView() {
                // 1. Remove the custom view
                FrameLayout decor = (FrameLayout) getWindow().getDecorView();
                decor.removeView(mCustomView);
                mCustomView = null;

                // 2. Restore the state to it's original form
                getWindow().getDecorView()
                        .setSystemUiVisibility(mOriginalSystemUiVisibility);
                setRequestedOrientation(mOriginalOrientation);

                mCustomViewCallback.onCustomViewHidden();
                mCustomViewCallback = null;

            }


        });


}
再加上这个

private View mCustomView;
    private int mOriginalSystemUiVisibility;
    private int mOriginalOrientation;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
试试这个代码

private class MyChrome extends WebChromeClient {

    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    MyChrome() {}

    public Bitmap getDefaultVideoPoster()
    {
        if (mCustomView == null) {
            return null;
        }
        return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {
        if (this.mCustomView != null)
        {
            onHideCustomView();
            return;
        }
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        getWindow().getDecorView().setSystemUiVisibility(3846);
    }
}
        myWebView.setWebChromeClient(new MyChrome());
并添加此代码

private class MyChrome extends WebChromeClient {

    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    MyChrome() {}

    public Bitmap getDefaultVideoPoster()
    {
        if (mCustomView == null) {
            return null;
        }
        return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {
        if (this.mCustomView != null)
        {
            onHideCustomView();
            return;
        }
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        getWindow().getDecorView().setSystemUiVisibility(3846);
    }
}
        myWebView.setWebChromeClient(new MyChrome());

不知道是否有帮助,在一个老项目(大约3年后)中,我发现在网络视图上显示youtube视频太麻烦了。使用YouTubeAndroidPlayerApi解决了我所有的问题,即使是全屏效果也很有魅力。它只是通过gradle导入,或者在项目上放置一个jar库并使用这些方法。我在构建模块上使用via.jar:
编译文件('libs/YouTubeAndroidPlayerApi.jar')
。注意:与所有其他谷歌API一样,你必须在youtube上注册才能在你的应用程序上运行。祝你好运。谢谢你的反馈。但我想在不使用YouTubeAndroidPlayerApi的情况下创建此应用程序。在这里,当播放视频时,我会在我的网络视图上看到全屏选项,但当我点击全屏选项时,它会显示白色屏幕,音频仍在后台播放。祝你好运。我仍然建议您使用GoogleAPI来实现这一点。有成千上万的android设备/系统在运行,每一个都有自己的独特之处。相信我:即使是谷歌也无法处理这一切,关于我们这些可怜的开发者和我们“独立”的努力该怎么说。。。有一件事你可以肯定:这是一个设备/系统问题。如果你只在5台设备上试用你的应用程序,或者在同一台设备上试用不同android api风格的应用程序,你会惊讶于每台设备的运行方式都不同——即使在EMU中,你也会发现它们是一样的。有了谷歌的api,这至少可以减少一点。什么是2130837573?