当前位置: 首页 > 知识库问答 >
问题:

YouTube视频未在WebView中播放

齐承运
2023-03-14
public class MainActivity extends Activity {

    private WebView mWebView;  

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

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());

    }

private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
        webview.loadUrl(url);
        return true;
    }}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
        {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }   }
<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

共有1个答案

令狐新翰
2023-03-14

似乎是在WebView中播放youtube视频和在WebView-Android中不播放youtube视频的重复

为了使它通过WebView工作,我使用了以下两个步骤(4.2.2/Nexus4):

>

  • ShouldoverrideurlLoading上,我添加了WebView.SetWebChromeClient(新建WebChromeClient());

    > 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" >
    
    <FrameLayout
              android:id="@+id/fullscreen_custom_content"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FF000000"/>
    
    <LinearLayout 
              android:id="@+id/linearlayout"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"> 
    
         <WebView
               android:id="@+id/webView"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent" />
    
    </LinearLayout>
    </RelativeLayout>
    

    在MainActivity类中:

    public class MainActivity extends Activity {
    
    private WebView mWebView;  
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mContentView = (LinearLayout) findViewById(R.id.linearlayout);
        mWebView = (WebView) findViewById(R.id.webView);
        mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
    
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
    
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
    
    private class HelloWebViewClient extends WebViewClient  {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.setWebChromeClient(new WebChromeClient() {
    
                private View mCustomView;
    
                 @Override
                public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
                {
                    // if a view already exists then immediately terminate the new one
                    if (mCustomView != null)
                    {
                        callback.onCustomViewHidden();
                        return;
                    }
    
                    // Add the custom view to its container.
                    mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
                    mCustomView = view;
                    mCustomViewCallback = callback;
    
                    // hide main browser view
                    mContentView.setVisibility(View.GONE);
    
                    // Finally show the custom view container.
                    mCustomViewContainer.setVisibility(View.VISIBLE);
                    mCustomViewContainer.bringToFront();
                }
    
            }); 
    
          webview.loadUrl(url);
    
          return true;
        }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
        {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    
    } 
    }
    

    EDIT2-添加后退按钮支持

    public class MainActivity extends Activity {
    
    private WebView mWebView;  
    private LinearLayout mContentView;
    private FrameLayout mCustomViewContainer;
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
    
    private WebChromeClient mWebChromeClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mContentView = (LinearLayout) findViewById(R.id.linearlayout);
        mWebView = (WebView) findViewById(R.id.webView);
        mCustomViewContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
    
        mWebChromeClient = new WebChromeClient() {
    
    
             @Override
            public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
            {
                // if a view already exists then immediately terminate the new one
                if (mCustomView != null)
                {
                    callback.onCustomViewHidden();
                    return;
                }
    
                // Add the custom view to its container.
                mCustomViewContainer.addView(view, COVER_SCREEN_GRAVITY_CENTER);
                mCustomView = view;
                mCustomViewCallback = callback;
    
                // hide main browser view
                mContentView.setVisibility(View.GONE);
    
                // Finally show the custom view container.
                mCustomViewContainer.setVisibility(View.VISIBLE);
                mCustomViewContainer.bringToFront();
            }
    
             @Override
             public void onHideCustomView()
             {
                 if (mCustomView == null)
                     return;
    
                 // Hide the custom view.
                 mCustomView.setVisibility(View.GONE);
                 // Remove the custom view from its container.
                 mCustomViewContainer.removeView(mCustomView);
                 mCustomView = null;
                 mCustomViewContainer.setVisibility(View.GONE);
                 mCustomViewCallback.onCustomViewHidden();
    
                 // Show the content view.
                 mContentView.setVisibility(View.VISIBLE);
             } 
        };
    
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
    
        mWebView.loadUrl("http://www.google.com");
        mWebView.setWebViewClient(new HelloWebViewClient());
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    
    
    private class HelloWebViewClient extends WebViewClient  {
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.setWebChromeClient(mWebChromeClient);   
            webview.loadUrl(url);
    
          return true;
        }
    }
    
    @Override
    protected void onStop() {
    
        super.onStop();
        if (mCustomView != null)
        {
            if (mCustomViewCallback != null)
                mCustomViewCallback.onCustomViewHidden();
            mCustomView = null;
        }
    
    }
    
    @Override
    public void onBackPressed() {
    
        super.onBackPressed();
         if (mCustomView != null)
         {
             mWebChromeClient.onHideCustomView();
         } else
         {
             finish();
         }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
        {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    
    }   
    }
    

  •  类似资料:
    • 有人可以帮助使用这个代码来播放嵌入网站的youtube视频吗?我试过各种方法,包括硬件加速=True。 webview显示了youtube页面和缩略图,但点击它们时什么也不会发生。 在某些硬件加速的设备上,它只播放声音,但不播放视频。谢谢大家!

    • 是否可以从webview播放youtube视频,就像我从浏览器访问youtube网站时视频开始播放时一样,它在全屏播放视频。 以下是我到目前为止的代码:

    • 我是绑定播放YouTube视频在WebView,WebView显示视频的第一个外观与播放按钮,但点击播放按钮后开始进度条和2-3秒后停止进度条和屏幕空白与黑色。 Image1:带播放按钮的视频首次查看 Image2:点击播放按钮后,屏幕变为空白。

    • 问题是我可以加载YouTube网站,浏览视频, 但当我点击播放视频时,什么也不会发生。 视频就像被选中一样会出现橙色闪光, 谢谢

    • 我正在制作RSS阅读器应用程序,我从URL获取RSS数据,RSS数据可以包含到YouTube视频链接。 下面是一个链接到youtube vide的例子: 而当我运行我的应用程序时,没有视频,全是黑色的,我无法播放。