如何嵌入YouTube视频在Android中的WebView? [英] How to embed Youtube video in Android WebView?

查看:1327
本文介绍了如何嵌入YouTube视频在Android中的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想嵌入在我的Andr​​oid应用程序一个YouTube视频。它有一个导航抽屉,当这个抽屉被打开,视频必须继续打球。我想我的应用程序可能是能够播放视频与版权限制,例如,从 VEVO 。我的第一次尝试是简单地加载嵌入URL( https://www.youtube.com/embed/VIDEO_ID )在一个浏览器客户端一个web视图,但对于许多视频,例如,从VEVO,如,我看到这条消息:

这部影片含有VEVO的内容。它是由播放在特定网站上受限制

要解决这个错误,我决定使用的Youtube API

这11b的可实现之间,我已经尝试了这些:

Android的API有,我可以播放视频具有上述限制的优势,但它被丢弃,因为它的默认行为是玩家停止抽屉打开时的录像。

通过iframe的API,我可以播放视频的抽屉打开时,而不是与限制。

我的最后一个试探性的是使用JavaScript API。目前,我也有同样情况的iframe,那就是,我不能和限制播放视频。

不过,我觉得这是找到一个解决的正确途径。在参考,他们建议使用的 SWFObject的,但据我所知,没有FLAH适用于移动,就像暴露<一href="http://stackoverflow.com/questions/19362049/setpluginsenabled-not-exist-for-webview/19364273#19364273">this回答。因此,有在我的WebView embeding视频的​​另一种方式?

和另一个疑问是:如何移动网站可以从YouTube上播放视频,甚至用某网站的限制?请您理解,谁是问这里有一个Android开发者没有太多经验的JavaScript。

目前,我有这块code:

布局,其中将被载入播放器:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

    &LT;的WebView
        机器人:ID =@ + ID / web_view
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/&GT;

&LT; /的FrameLayout&GT;
 

片段膨胀此布局:

 公开查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

    查看查看= inflater.inflate(R.layout.fragment,NULL);

    的WebView web视图=(web视图)view.findViewById(R.id.web_view);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(真正的);

    webView.loadUrl(文件:///android_asset/player_webview.html);
    webView.setWebViewClient(新CustomClient类());

    返回查看;
}

私有类CustomClient类扩展WebViewClient {
    @覆盖
    公共布尔shouldOverrideUrlLoading(的WebView的WebView,字符串URL){
        webview.setWebChromeClient(新WebChromeClient());
        webview.loadUrl(URL);
        返回true;
    }
}
 

HTML文件

 &LT;!DOCTYPE HTML&GT;
&LT; HTML&GT;
&LT; HEAD&GT;

  &LT;脚本类型=文/ JavaScript的SRC =./ SWFObject的/ swfobject.js&GT;&LT; / SCRIPT&GT;

  &LT; D​​IV ID =ytapiplayer&GT;
      您需要安装Flash Player 8 +和启动Javascript才能观看该视频。
  &LT; / DIV&GT;

  &LT;脚本类型=文/ JavaScript的&GT;

    VAR PARAMS = {allowScriptAccess的:永远};
    VAR的ATT = {ID:myytplayer};
    swfobject.embedSWF("http://www.youtube.com/v/G90AgiAMM6k?enablejsapi=1&playerapiid=ytplayer&version=3",
                       ytapiplayer,425,356,8,NULL,NULL,参数,可以的ATT);

  &LT; / SCRIPT&GT;
&LT; /头&GT;
&LT; / HTML&GT;
 

解决方案

我认为,这已经被问过类似的问题可能是对您有所帮助,并可能是一个很好的起点。但我要发布第二个链接可能更多的帮助。

<一个href="http://stackoverflow.com/questions/6323169/play-youtube-html5-embedded-video-in-android-webview">Play的Youtube HTML5视频嵌入在Android中的WebView

另外,我在一个单独的网站上找到这条评论,可能是你神奇的答案:

自定义播放器,您都在提最有可能采用的是相当知名的把戏;嘴硬的YouTube到提供一个MP4版本的视频,渐进式下载它,并通过本地的HTTP服务器的MPMoviePlayerController服务回来

的限制是基于访问有问题的视频时,您正在使用的IP地址进行反向查找。从美国国内使用代理,你会看到它突然播放没有任何问题。<一href="http://w3facility.org/question/embed-youtube-videos-with-contains-content-from-it-is-restricted-from-playback-on-certain-site/" rel="nofollow">http://w3facility.org/question/embed-youtube-videos-with-contains-content-from-it-is-restricted-from-playback-on-certain-site/

我希望这有助于!

I want to embed a youtube video in my Android app. It has a Navigation Drawer, and when this drawer is opened, the video must to keep playing. And I'd like my app could be capable of playing video with copyright restriction, for example, videos from Vevo. My first try was simply load the embed url (https://www.youtube.com/embed/VIDEO_ID) in a WebView with a Chrome Client, but for many videos, for example that from Vevo, like this, I see this message:

This Video Contains content from Vevo. It is restricted from playback on certain sites

To handle this error, I decided to use the Youtube API.

Between the available implementations of this lib, I've tried these:

The Android API has the advantage that I can play video with the mentioned restriction, but it was discarded because its default behavior is the player stops the video when the Drawer is opened.

With the Iframe API, I can play videos when the Drawer opens, but not that with the restriction.

My last tentative is using the Javascript API. Currently, I have the same situation that iframe, that is, I can't play videos with restriction.

However I think this is the right way of finding a solution. In the api reference, they recommend using the SWFObject, but as far as i know, there is not flah available for mobile, like exposed in this answer. So, there is another way of embeding video in my WebView?

And another doubt is: How do mobile sites can play video from youtube, even that with the "certain sites" restriction? Please understand that who is asking here is an Android developer not much experienced with Javascript.

Currently, I have this piece of code:

Layout where will be loaded the player:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Fragment that inflates this layout:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment, null);

    WebView webView = (WebView) view.findViewById(R.id.web_view);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.loadUrl("file:///android_asset/player_webview.html");
    webView.setWebViewClient(new CustomClient());

    return view;
}

private class CustomClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl(url);
        return true;
    }
}

HTML file

<!DOCTYPE html>
<html>
<head>

  <script type="text/javascript" src="./swfobject/swfobject.js"></script>

  <div id="ytapiplayer">
      You need Flash player 8+ and JavaScript enabled to view this video.
  </div>

  <script type="text/javascript">

    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/G90AgiAMM6k?enablejsapi=1&playerapiid=ytplayer&version=3",
                       "ytapiplayer", "425", "356", "8", null, null, params, atts);

  </script>
</head>
</html>    

解决方案

I think that a similar question that has been asked might be of help to you and might be a good starting point. But the second link I am going to post might be of more help.

Play Youtube HTML5 embedded Video in Android WebView

Also, I found on a separate website this comment that might be your magic answer:

"The custom player you are mentioning most likely uses a rather well-known trick; fooling youtube into providing an MP4-version of that video, download it progressively and serve it back via a local http-server to MPMoviePlayerController.

The restriction is based on a reverse lookup of the IP address you are using when accessing the video in question. Use a proxy from within the US and you will see that it suddenly plays without any problem." http://w3facility.org/question/embed-youtube-videos-with-contains-content-from-it-is-restricted-from-playback-on-certain-site/

I hope this helps!!

这篇关于如何嵌入YouTube视频在Android中的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆