I want to embed a YouTube video in an Android app. It has a navigation box , and when this box is open, the video should continue. And I would like my application to be able to play videos with copyright restrictions, for example, videos from Vevo . My first attempt was to load a downloadable url ( https://www.youtube.com/embed/VIDEO_ID) into a WebView with a Chrome client, but for many videos, for example, from Vevo, for example this , I see this message:
This Video Contains content from Vevo. It is restricted from playback on certain sites
To deal with this error, I decided to use the Youtube API .
Between the available implementations of this lib, I tried these:
The Android API has the advantage that I can play the video with the specified restriction, but it was dropped because its default behavior is that the player stops the video when the box is opened.
With the Iframe API, I can play videos when the Box opens, but is not limited to this.
My last example is using the Javascript API. Currently, I have the same situation as iframe, that is, I can not play videos with restrictions.
However, I believe that this is the right way to find a solution. In the api link , they recommend using SWFObject , but as far as I know, flah does not exist for mobile devices, as shown in this answer . So, is there another way to embed videos in my WebView?
: youtube, " "? , , , , Android, Javascript.
:
, :
<?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>
, :
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
<!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>