I want to play a YouTube video in a WebView for Amazon Fire TV.
As of today, there is no official API for playing YouTube videos on Fire OS (link), so I tried to get it working with Android's WebView. In the Android WebView documentation it is written, that the app needs to have hardware acceleration turned on and that the WebView needs to have a WebChromeClient in order to playback YouTube videos.
I tried to get it work but when I start a YouTube video, than I only see the loading spinner in fullscreen mode.
Here is my code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Log.i("fire-tv", "Show custom view");
super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView) {
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
setContentView(video);
video.start();
}
}
}
@Override
public void onHideCustomView() {
Log.i("fire-tv", "Hide custom view.");
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
Log.i("fire-tv", "Loading: " + html);
webview.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
private String getHTML() {
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 100%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
+ "h11u3vtcpaY"
+ "?fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
return html;
}
I've written a class to play a youtube video on Fire TV and it also works on the kindle fire as well. As you know, Youtube doesn't have an offical api and we have to resort to using webviews for this. The class I've pasted here has a lot of stuff besides webviews that you might be curious about so I will explain. First of all, you will find that a YouTube playing webview will keep playing when you leave your Activity unless you tell it to stop. So the solution people tell others is to use the onPause and pauseTimers methods to shut up the video. But there is a nasty problem that Amazon has foisted on us and that is the audio and video resources are not released to the rest of the operating system (not a problem with Google Android Lolipop ie Nexus Player). When the resources are held by your app, prime video will not play and amazon will not allow your app on the store. They know darned well we have to use webviews for youtube, so they gave us this little bit of advice:
Amazon source
Like AVGN would say "WHAT WERE THEY THINKING??"
You wouldn't believe how just force killing an app or Activity would lead to tons of problems.. well I'm sure you do, cause you got a few thousand points more than myself.
The only solution I could find is to load a very short 720p HD clip (must have audio!) in the onPause method. I combined it with webView.onPause and .pauseTimers to be sure...
If you use this class please copy my blank.mp4 file to your own server because it might not be there in a month.
Oh but there is more. So you get your YouTube file in your webView... Piece of cake right? Wrong, it doesn't play because in webViews, Html5 usually never auto plays. You cannot set the url to auto play it makes no difference. So my solution was to force a click using java trickery. The way it works is that the webview doesn't load immediately, it takes a variable amount of time for it to fully load. After it does, the video shows a play button icon at the center of the screen. The little remote can't touch it for some reason. But I put in code to force a click in the center of the webview.
Oh and there is one more thing, you must handle audio focus or amazon will not allow your app in the store. Pandora could play in the background unless you handle that.
why Amazon why