2013-07-17 221 views
4

YouTube視頻不能在WebView中播放。YouTube視頻不能在WebView中播放

它是我的第一個應用程序,我想做一個web視圖。 當我打開YouTube時,視頻不能播放,正在加載但不能播放。 正在加載。 非常感謝您的幫助。

的Java:

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:

<?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" 
/> 
+0

http://stackoverflow.com/questions/15768837/playing-html5-video-on-fullscreen-in-android-webview – EpicPandaForce

回答

21

好像的play youtube video in WebView重複和YouTube Video not playing in WebView - Android

爲了使它通過的WebView工作,我用了以下兩個步驟(4.2版本。 2/Nexus 4):

  1. shouldOverrideUrlLoading我加webview.setWebChromeClient(new WebChromeClient());

  2. AndroidManifest.xmlactivity標籤我加android:hardwareAccelerated="true"

AndroidManifest.xml中還應該包含Internet權限。

儘管通過WebView播放視頻的確切步驟可以針對設備和Android版本,但也可以使用其他WebView替代方法,例如VideoView或使用Youtube Android Player API。

EDIT1

至於暫停和全屏,這其實是我在開頭提到的第二個鏈接。爲了更容易,我發佈了在我的Nexus上工作的代碼。

  1. 在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> 
    
  2. 在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); 

} 
} 
+0

但它沒有工作。 – omer341

+1

@ omer341你試圖在哪個Android版本上運行它或模擬它?你有什麼嘗試?添加了'webview.setWebChromeClient(new WebChromeClient(){});'和硬件加速標記對您的情況沒有用處? –

+1

我試圖在我的平板電腦上運行它在Android 4.1.2版本上。 – omer341

0

添加webchrome客戶解決了我已經看到了這個問題,

web = (WebView) vi.findViewById(R.id.offer_webView1); 

     web.loadUrl(getResources().getString(R.string.videolink)); 

     web.getSettings().setJavaScriptEnabled(true); 
     // web.getSettings().setDomStorageEnabled(true); 

     web.getSettings().setAllowContentAccess(true); 
     WebSettings webSettings = web.getSettings(); 
     webSettings.setPluginState(WebSettings.PluginState.ON); 
     webSettings.setUseWideViewPort(true); 
     webSettings.setLoadWithOverviewMode(true); 
     web.canGoBack(); 
     web.setWebChromeClient(new WebChromeClient() {}); 
+1

不適合我 –