2012-10-23 44 views
15

我目前正在開發基於網站的Android應用程序。 iOS應用程序已經存在,我必須尊重一些代碼的統一性。iframe視頻不會在Android Web視圖中進入全屏模式

一切都差不多了,但我發現了一個有趣的問題:使用的WebView時(我沒有顯示在頁面上的任何控制),用於與一個iframe視頻(的Youtube,土豆網)的頁面時,它贏得了」即使我按下播放器的按鈕,也不能全屏顯示。

我已經嘗試了幾乎所有在這裏找到的東西,但它只涉及應用程序,我知道你需要顯示哪些頁面。

下面是應用程序的一部分webActivity代碼:

public class WebActivity extends Activity { 
    String targetURL = ""; 
    String title = ""; 
    WebView wv; 

    @Override 
    public void onResume() { super.onResume(); CookieSyncManager.getInstance().startSync(); } 
    @Override 
    public void onPause() { super.onPause(); CookieSyncManager.getInstance().stopSync(); } 

    /** Called when the activity is first created. */ 
    @SuppressLint("SetJavaScriptEnabled") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     //getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     CookieSyncManager.createInstance(getApplicationContext()); 
     CookieSyncManager.getInstance().startSync(); 
     CookieManager.getInstance().setAcceptCookie(true); 
     /** 
     * TODO: WebView Cookie management. 
     * Right now, a cookie is hardcoded here into the WebView instead of getting it from the API called by HttpClient when retrieving the JSON. 
     * Need to make things cleaner. 
     */ 
     CookieManager.getInstance().setCookie("http://www.blabla.fr/mobile/","gbapi=1; Domain=.www.blabla.fr"); 
     /** 
     * Get parameters 
     */ 
     Bundle b = getIntent().getExtras(); 
     if(b != null) 
     { 
      targetURL = b.getString("url"); 
      title = b.getString("title"); 
     } 

     setTitle(title); 
     setContentView(R.layout.activity_webview); 

     wv = (WebView) findViewById(R.id.webview); 

     WebSettings wvSettings = wv.getSettings(); 

     // WebView options 
     wvSettings.setDefaultTextEncodingName("utf-8"); 
     wvSettings.setJavaScriptEnabled(true); 
     wvSettings.setPluginState(PluginState.ON); 
     wvSettings.setJavaScriptCanOpenWindowsAutomatically(true); 
     wvSettings.setBuiltInZoomControls(true); 
     final Activity activity = this; 
     wv.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
       activity.setProgress(progress * 100); 
      } 
     }); 

     wv.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, "Oh snap! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     wv.loadUrl(targetURL); 
    } 
} 

感謝您的幫助。

回答

2

您將需要創建一個自定義WebChromeClient,以處理onShowCustomView的兩個版本(此回調的新版本在API級別14中引入)以及onHideCustomView。從本質上來說,當你嘗試播放全屏視頻時,你會看到VideoView的一些變化。您需要將其附加到全屏FrameLayout並將其粘貼到佈局層次結構的根目錄以覆蓋整個屏幕。一旦完成,您需要再次移除它。

這裏是我用來播放視頻

private class DerpChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener { 
    //@Override 
    public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) { 
     log.warn("onShowCustomView"); 
     showCustomView(view, callback); 
    } 

    private View mVideoProgressView; 

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

     activity.removeFullscreenView(); 
     webView.setVisibility(View.VISIBLE); 

     try { 
      mCustomViewCallback.onCustomViewHidden(); 
     } catch (NullPointerException npe) { 
      // occasionally Android likes to freak out and throw an unhandled NPE if it can't play the video 
      // therefore we are not going to do anything but eat this exception so it fails gracefully 
     } 

     mCustomView = null; 
     mVideoView = null; 
    } 

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

    private void showCustomView(View view, CustomViewCallback callback) { 
     if (mCustomView != null) { 
      callback.onCustomViewHidden(); 
      return; 
     } 

     mCustomView = view; 
     mCustomViewCallback = callback; 
     webView.setVisibility(View.GONE); 

     if (view instanceof FrameLayout) { 
      if (((FrameLayout)view).getFocusedChild() instanceof VideoView) { 
       mVideoView = (VideoView)((FrameLayout)view).getFocusedChild(); 
      } 
     } 

     view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 
     view.setBackgroundColor(Color.BLACK); 
     activity.displayFullscreenView(view); 
    } 

    @Override 
    public boolean onConsoleMessage(ConsoleMessage cm) { 
     log.warn("Console Message: " + cm.message() + " on line " + cm.lineNumber() + " of " + cm.sourceId()); 
     return super.onConsoleMessage(cm); 
    } 

    @Override 
    public void onProgressChanged(WebView view, int newProgress) { 
     super.onProgressChanged(view, newProgress); 

     if (newProgress < 100) { 
      if (loadingProgress.getVisibility() == ProgressBar.GONE) { 
       loadingProgress.setVisibility(ProgressBar.VISIBLE); 
      } 
      loadingProgress.setProgress(newProgress); 
     } else if (newProgress >= 100) { 
      loadingProgress.setVisibility(ProgressBar.GONE); 
     } 
    } 

    @Override 
    public View getVideoLoadingProgressView() { 
     if (mVideoProgressView == null) { 
      LayoutInflater inflater = LayoutInflater.from(KnowledgeBaseFragment.this.getActivity().getApplicationContext()); 
      mVideoProgressView = inflater.inflate(R.layout.video_loading_progress, null); 
     } 

     return mVideoProgressView; 
    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     this.onHideCustomView(); 
    } 
} 

注意,我是一個片段中這樣一個版本,所以,使之真正全屏我必須緊密地結合它與活動,以便它可以連接FrameLayout在整個視圖層次結構的根部,而不僅僅是片段的。

下面是這些功能:

@Override 
public void displayFullscreenView(View customView) { 
    parentLayout.addView(customView); 
    this.customView = customView; 
} 

@Override 
public void removeFullscreenView() { 
    if (customView != null) { 
     customView.setVisibility(View.GONE); 
     parentLayout.removeView(customView); 
    } 

    customView = null; 
} 

這裏有像你這樣的一個問題:WebView and HTML5 <video>

+1

我還沒有在幾個月看着這個代碼。大量的半手術和剩餘變量。 ><清理時間。 – MattC

+1

謝謝!我不得不調整一下代碼,但可以使用它。 – Pascal