2012-10-23 50 views
15

Tôi hiện đang làm việc trên một ứng dụng Android dựa trên một trang web. Ứng dụng iOS đã tồn tại và tôi phải tôn trọng một số mã cho tính đồng nhất.video iframe sẽ không chuyển sang chế độ toàn màn hình trong chế độ xem web của Android

Mọi thứ sắp hoàn thành, nhưng tôi vừa phát hiện ra một vấn đề thú vị: khi sử dụng chế độ xem web (tôi không có bất kỳ kiểm soát nào trên trang được hiển thị) cho một trang có video iframe (Youtube, Dailymotion), nó đã thắng ' t đi toàn màn hình, mặc dù tôi đang nhấn nút của trình phát.

Tôi đã thử khá nhiều mọi thứ được tìm thấy ở đây, nhưng nó chỉ đề cập đến các ứng dụng mà tôi biết bạn cần hiển thị những trang nào.

Dưới đây là các mã cho phần webActivity của ứng dụng:

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); 
    } 
} 

Cảm ơn sự giúp đỡ nào.

Trả lời

2

Bạn sẽ cần tạo WebChromeClient tùy chỉnh xử lý cả hai phiên bản của onShowCustomView (phiên bản mới của cuộc gọi lại này đã được giới thiệu ở cấp API 14) cũng như onHideCustomView. Về cơ bản những gì sẽ xảy ra là khi bạn cố gắng phát video toàn màn hình, bạn sẽ được cung cấp một chế độ xem là một số biến thể của VideoView. Bạn cần phải đính kèm nó vào FrameLayout toàn màn hình và dán nó vào thư mục gốc của phân cấp bố cục của bạn để phủ lên toàn bộ màn hình. Sau khi hoàn tất, bạn cần phải xóa lại.

Đây là một phiên bản tôi đang sử dụng để chơi video

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(); 
    } 
} 

Lưu ý rằng tôi đang làm điều này trong một đoạn, vì vậy để làm cho nó thực sự toàn màn hình tôi phải chặt cặp đôi nó với các hoạt động để nó có thể đính kèm FrameLayout tại thư mục gốc của toàn bộ hệ thống phân cấp khung nhìn, không chỉ là phân đoạn.

Dưới đây là những chức năng:

@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; 
} 

Dưới đây là một câu hỏi như của bạn: WebView and HTML5 <video>

+1

tôi đã không nhìn mã này trong một vài tháng. Rất nhiều phẫu thuật nửa và các biến còn sót lại. > MattC

+1

Cảm ơn! Tôi đã phải tinh chỉnh một chút mã, nhưng có thể sử dụng nó. – Pascal

Các vấn đề liên quan