Android中的webView视频,仍然无法使其正常运行

时间:2011-08-15 05:38:46

标签: android video webview

所以,我还在拔头发。我的代码如下。我只是想让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);
}

}

1 个答案:

答案 0 :(得分: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;
             }
       }
});