1

我想在 WebView 上禁用 Youtube 嵌入视频,最好用指向 Youtube 页面的链接替换它们(单击时将打开浏览器选择器)...

这是我的(相当通用的)webview:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parodia);

    BlogView = (WebView) findViewById(R.id.blogview);
    BlogView.setWebViewClient(new CustomClient());
    WebSettings webSettings = BlogView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    BlogView.loadUrl("http://test.blogspot.com/?m=1");
}

编辑:我正在寻找这样的东西,对如何在android webview上实现它有任何帮助吗?

4

1 回答 1

2
$(document).ready(function() {
    $('iframe').each(function() {
        var src = $(this).attr('src');
        // Replace youtube vids
        var ytprefix = "http://www.youtube.com/embed/";

        if(src.indexOf(ytprefix) != -1) {
            replaceYT(this, src.substring(ytprefix.length));
        }
    });

    $('object').each(function() {
        var srcel = $('param[name="src"]', this);
        var src = $(srcel).attr('value');
        // Replace youtube vids
        var ytprefix = "http://www.youtube.com/v/";
        if(src.indexOf(ytprefix) != -1) {
            replaceYT(this, src.substring(ytprefix.length));
        }
    });
});

function replaceYT(el, code) {
    if(code.indexOf("/") != -1) {
        code = code.substring(0, code.indexOf("/"));
    }
    if(code.indexOf("?") != -1) {
        code = code.substring(0, code.indexOf("?"));
    }
    var atag = $("<a href='vnd.youtube:" + code +"'><img class='youtubeimg' src='file:///android_asset/youtube-play-button.png' style='background:url(http://img.youtube.com/vi/" + code + "/0.jpg)'/></a>");
    $(el).replaceWith(atag);
}

Just load in jquery and this script. This will replace youtube iframes and object tags with a screenshot from the video and a link to youtube. Tested on a SE Xperia X10 & Nexus 7.

Set up your webview to handle youtube like this: WebViewClient mWebClient = new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("http://www.youtube.com") || url.startsWith("vnd.youtube")){
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
        return false;
    }

};
于 2012-10-18T10:19:36.670 回答