2011-08-15 56 views
0

所以,我仍然拔出我的頭髮。我的代碼如下。我只是想web視圖打開視頻和它仍然沒有工作.....在Android的webView視頻,仍然無法讓它工作

package com.AFMoB.WebView001; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.webkit.WebSettings; 

public class WebView001 extends Activity { //** Called when the activity is first created. */ 
public WebView webView; //DECLARE webview variable outside of onCreate function so we can access it in other functions (menu) 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    webView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml 
    WebView webView = (WebView)findViewById(R.id.webview); 
    WebSettings webSettings = webView.getSettings(); // Fetches the WebSettings import 
    webSettings.setPluginsEnabled(true); // Allows plugins to run which are normally disabled in webView 
    webView.getSettings().setBuiltInZoomControls(true); // Allows the Android built in zoom control 
    webView.getSettings().setSaveFormData(true); 
    webView.getSettings().setSupportMultipleWindows(true); 
    webView.getSettings().setPluginsEnabled(true); 
    webView.getSettings().setAllowFileAccess(true); // To allow file downloads/streams such as mp4, mpeg, and 3gp files 
    webView.getSettings().setJavaScriptEnabled(true); // Enables HTML Javascript to run in webview 
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webView.getSettings().setSupportZoom(true); // Support the zoom feature 
    webView.getSettings().setSavePassword(true); // Allow users to save passwords in forms 
    webView.setWebViewClient(new WebViewClient() { // Opens web links clicked by user in the webview 
    @Override 
    public void onReceivedError(WebView view, int errorCode, 
     String description, String failingUrl) { // Handle the error 
      } 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
         view.loadUrl(url); 
         return true; 
        } 
       }); 
    webView.loadUrl("http://broken-links.com/tests/video/"); // Modify the URL for webview here 
} 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu 
    menu.add(0, 1, 0, "Back"); 
    menu.add(0, 2, 0, "Refresh"); 
    menu.add(0, 3, 0, "Forward"); 
    return true; // End of menu configuration 
} 
public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item 
    switch (item.getItemId()){ 
     case 1: //If the ID equals 1, go back 
      webView.goBack(); 
     return true; 
     case 2 : //If the ID equals 2, refresh 
      webView.reload(); 
     return true; 
     case 3: //If the ID equals 3, go forward 
      webView.goForward(); 
     return true; 
     } 
    return false; 
    } 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history 
     webView.goBack(); 
     return true; 
    } // If it wasn't the BACK key or there's no web page history, bubble up to the default 
     // system behavior (probably exit the activity) 
    return super.onKeyDown(keyCode, event); 
} 

}

+0

什麼是「它仍然沒有工作.. ......「的意思是?有錯誤嗎? –

+0

沒有錯誤。該頁面在加載時點擊視頻時應該簡單地打開本地媒體播放器。它絕對沒有。 –

+0

我所尋找的是專門爲web視圖蹦出所有的視頻與本地媒體播放器 –

回答

0

嘗試下面的代碼

private View myView = null; 
private CustomViewCallback myCallback = null; 

webView.setWebChromeClient(new WebChromeClient(){ 
     @Override 
     public void onShowCustomView(View view, CustomViewCallback callback) { 
      if (myCallback != null) { 
        myCallback.onCustomViewHidden(); 
        myCallback = null ; 
        return; 
      } 
      ViewGroup parent = (ViewGroup) mWebView.getParent(); 
      String s = parent.getClass().getName(); 

      parent.removeView(mWebView); 
      parent.addView(view); 
      myView = view; 
      myCallback = callback; 
     } 
     public void onHideCustomView() { 

      if (myView != null) { 

        if (myCallback != null) { 
         myCallback.onCustomViewHidden(); 
         myCallback = null ; 
        } 

        ViewGroup parent = (ViewGroup) myView.getParent(); 
        parent.removeView(myView); 
        parent.addView(mWebView); 
        myView = null; 
      } 
     } 
}); 
相關問題