2011-11-28 130 views
1

我想播放從SD卡的web視頻視頻。我GOOGLE了,但無法得到解決方案。我已經試過這種方式:無法播放視頻在HTML使用從SD卡的webview

public class WebActivity extends Activity { 
private WebView mWebView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.helpview); 
    mWebView =(WebView) findViewById(R.id.webview); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.getSettings().setPluginsEnabled(true); 
    mWebView.getSettings().setAllowFileAccess(true); 
    mWebView.getSettings().setSupportZoom(true); 
    mWebView.getSettings().setBuiltInZoomControls(true); 

    String html ="<html><head></head><body>Playing Video<object width=\"550\" height=\"400\"> <param name=\"movie\" value=\"file://"+Environment.getExternalStorageDirectory()+"/test-embeded.swf\"> <embed src=\"file://"+Environment.getExternalStorageDirectory()+"/test-embeded.swf"+"\" width=\"550\" height=\"400\"> </embed> </object></body></html>"; 
    String mimeType = "text/html"; 
    String encoding = "utf-8"; 

    mWebView.loadDataWithBaseURL("null", html, mimeType, encoding, ""); 


    } 
} 

我得到輸出爲空白屏幕。

我甚至已經安裝Adope的Flash Player 10.1.apk 但仍輸出類似,只有

有誰知道解決的辦法?

回答

1

我已經加載了一個web頁面中的html頁面,其中包含視頻標籤,我得到了輸出。 HTML文件在\文件夾中的資產,這是我的代碼,它可以幫助你,

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    WebView webView = (WebView) findViewById(R.id.webview); 
    String html = "<embed src=\"file:///android_asset/" + "demo.html" + " \"play=\"true\" loop=\"true\" width=\"100%\" height=\"100%\"> <embed>"; 
    webView.getSettings().setPluginState(WebSettings.PluginState.ON); 
    webView.getSettings().setPluginsEnabled(true); 
    webView.getSettings().setAllowFileAccess(true); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null); 

    WebViewClient webViewClient = new WebViewClient(); 
    webView.setWebViewClient(webViewClient); 

    webView.setWebViewClient(new WebViewClient(){ 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 
     } 


     @Override 
     public void onLoadResource(WebView view, String url) { 
       if (url.endsWith(".mp4")) { 
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri 
         .parse(url)); 
       intent.setDataAndType(Uri.parse(url), "video/*"); 
       view.getContext().startActivity(intent); 

      } 
      else 
      { 
       super.onLoadResource(view, url); 
      } 
     } 


    }); 
} 

而且我的HTML頁面,

<!DOCTYPE HTML> 
<html lang="en"> 
<head> 
<meta charset=utf-8> 
<title>Sample HTML5 Structure</title> 
<SCRIPT LANGUAGE="JavaScript"> 
     function playvideo(url) 
     { 

        window.location=url; 

     } 
</SCRIPT> 
</head> 
<body> 
<video src="file:///sdcard/video.mp4" width="300" onclick="playvideo('file:///sdcard/video.mp4')" height="150" controls> 
<p>If you are reading this, it is because your browser does not support the HTML5 video element.</p> 
</video> 
</body> 
</html> 
+0

太感謝你了......我會試試這個。 – prasanna

+0

@ nik431是否可以從資產文件夾中播放? –

0

一些事情可能出錯。

首先,檢查AndroidManifest.xml。你有收錄

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

接下來,檢查日誌(亞行logcat)中的錯誤,並張貼在這裏