WebView中的Youtube视频无法加载

时间:2011-12-12 14:18:52

标签: android video webview youtube

我已经在我的Activity中实现了一个WebView,因为我已经获得了许可

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

使用此代码:

    WebView mWebView;
    mWebView = (WebView) findViewById(R.id.ueber_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("file:///android_asset/MyTestPage.htm");

html 页面正在加载 * 正好 *,显示 视频< / strong>带有边框和视频中心的典型Youtube-Play-Button。但是当我时,视频 * 加载 *。

这是htm内容:

    <div id="playerFrame">
<iframe src="http://www.youtube.com/embed/MyURL" //have to hide the url, ends with something like "...j4Fs?rel=0"
frameborder="0" allowfullscreen id="playerPlaceHolder"></iframe>
</div>

css内容必须正确,因为它显示正确,这应该不是问题。

任何帮助?

1 个答案:

答案 0 :(得分:10)

现在它有效,结合了一些东西:

public class Videos extends Activity {

private WebView mWebView;
private String extra;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videos_layout);       

    extra = getIntent().getStringExtra("VideosId");        
    mWebView = (WebView) findViewById(R.id.videos_webview); 
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);

    final Activity activity = this;
    mWebView.setWebChromeClient(new WebChromeClient() {
      public void onProgressChanged(WebView view, int progress) {
        // Activities and WebViews measure progress with different scales.
        // The progress meter will automatically disappear when we reach 100%
        activity.setProgress(progress * 1000);
      }
    });

    mWebView.setWebViewClient(new MyOwnWebViewClient());   


    mWebView.loadUrl("file:///android_asset/Videos/"+extra+".htm");     
    mWebView.setWebViewClient(new MyOwnWebViewClient());      
}

//Ermöglicht das Zurücknavigieren im Browser mittels Back-Button
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

清单中的这些条目:

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name" android:hardwareAccelerated = "true">

但我不知道哪一个至少是必要的。此外,不可能跳过部分视频,我必须一次观看。有人知道解决方案吗?

MyOwnWebViewClient

import android.webkit.WebView;
import android.webkit.WebViewClient;
//Enables the possibility for the User to navigate in the WebView and prevents,
//that the Standard-Browser is invoked

public class MyOwnWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}