I wrote a class to play YouTube videos on Fire TV, and is also working on a kindling fire. As you know, Youtube does not have an official api, and we need to resort to using web views for this. The class I inserted here contains a lot of material besides webviews that may interest you, so I’ll explain. First of all, you will find that the YouTube web view will continue to play when you leave your activity, if you do not report it to stop. Therefore, people who offer solutions to other people should use the onPause and pauseTimers methods to shut the video down. But there is an unpleasant problem that Amazon is imposing on us, and that audio and video resources do not apply to the rest of the operating system (this is not a problem with Google Android Lolipop, i.e. Nexus Player).When resources are stored in your application, the main video will not play, and amazon will not allow your application in the store. They know well that we need to use webviews for youtube, so they gave us some advice:
: Android WebView HTML ?
A: , . , . onStop() :
Amazon
AVGN : " ?"
, . , , , , .
, , - 720p HD ( !) onPause. webView.onPause .pauseTimers, ...
, blank.mp4 , .
, . , YouTube -... ? , , webViews Html5 . URL , . , , java trickery. , , , - , . . - . , -.
, , , . , .
Amazon
package com.ohiovr.modules.youtube;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Timer;
import java.util.TimerTask;
public class youtubeKindleWebView extends Activity {
String StringYoutubeUrl;
WebView webView;
AFChangeListener hocusFocus;
private String videoBlanker = "http://ohiovr.com/church_files/blank.mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generic_web_view);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setUserAgentString("Mozilla/5.0(iPhone;U;CPUiPhoneOS4_0likeMacOSX;en-us)AppleWebKit/532.9(KHTML,likeGecko)Version/4.0.5Mobile/8A293Safari/6531.22.7");
webView.setWebViewClient(new Callback());
webView.setWebChromeClient(new WebChromeClient());
}
class AFChangeListener implements AudioManager.OnAudioFocusChangeListener {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean handled = false;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
forceAClick();
break;
case KeyEvent.KEYCODE_BUTTON_A:
handled = true;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = true;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = true;
break;
}
return handled || super.onKeyDown(keyCode, event);
}
public void forceAClick() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = width / 2;
float y = height / 2;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_DOWN,
x,
y,
metaState
);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("on touch", "touched down");
return false;
}
});
webView.dispatchTouchEvent(motionEvent);
}
});
}
}, 1);
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = width / 2;
float y = height / 2;
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("on touch", "touched up");
return false;
}
});
webView.dispatchTouchEvent(motionEvent);
}
});
}
}, 120);
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
hocusFocus = new AFChangeListener();
int result = am.requestAudioFocus(hocusFocus, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
@Override
public void onResume() {
super.onResume();
webView.onResume();
webView.resumeTimers();
StringYoutubeUrl = "https://www.youtube.com/embed/" + getIntent().getStringExtra("youtubeID");
webView.loadUrl(StringYoutubeUrl);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
@Override
protected void onPause() {
super.onPause();
webView.loadUrl(videoBlanker);
webView.pauseTimers();
webView.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}