在 Fire TV 中播放 youtube 视频的问题
Issue with playing youtube videos in Fire TV
我正在开发一个从后端获取 youtube 视频 ID 的应用程序,它应该自动播放队列中的视频而无需用户干预。但这些是我面对不同方法的挑战。
使用 Youtube API:- 这不受 FireTV 支持,我的应用程序未在亚马逊商店中获得批准。
使用网络视图:-
当我将视频 URL 用作“https://www.youtube.com/watch?v=ajMpIN6BvQE”时,youtube 视频会自动开始播放。但是我看到了对视频的推荐,无法自动删除这些推荐
当我将视频 URL 用作“https://www.youtube-nocookie。com/embed/ajMpIN6BvQE”时,它停止显示推荐,但这里的挑战是我无法自动播放视频。它迫使用户明确点击播放按钮。下面是我使用的代码
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class WebViewActivity extends Activity {
WebView webView;
WebChromeClient.CustomViewCallback customViewCallback;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
ChromeClient chromeClient = new ChromeClient();
webView.setWebChromeClient(chromeClient);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
// webView.loadUrl("https://www.youtube-nocookie.com/embed/ajMpIN6BvQE");
webView.loadUrl("https://www.youtube.com/watch?v=ajMpIN6BvQE");
webSettings.setMediaPlaybackRequiresUserGesture(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
@Override
public void onPause() {
webView.onPause();
super.onPause();
}
@Override
public void onDestroy() {
webView.destroy();
super.onDestroy();
}
@Override
public void onStop() {
super.onStop();
}
@Override
protected void onRestart() {
super.onRestart();
}
public void onBackPressed() {
webView.stopLoading();
finish();
}
@Override
protected void onStart() {
super.onStart();
}
public class ChromeClient extends WebChromeClient {
protected FrameLayout mFullscreenContainer;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
ChromeClient() {
}
public Bitmap getDefaultVideoPoster() {
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
@Override
public void onHideCustomView() {
((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
@Override
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}}
我能够通过 https://developers.google.com/youtube/iframe_api_reference 获得解决方案。添加了 Java 脚本代码并启用 Java 脚本与原生 android.
之间的交互
我正在开发一个从后端获取 youtube 视频 ID 的应用程序,它应该自动播放队列中的视频而无需用户干预。但这些是我面对不同方法的挑战。
使用 Youtube API:- 这不受 FireTV 支持,我的应用程序未在亚马逊商店中获得批准。
使用网络视图:-
当我将视频 URL 用作“https://www.youtube.com/watch?v=ajMpIN6BvQE”时,youtube 视频会自动开始播放。但是我看到了对视频的推荐,无法自动删除这些推荐
当我将视频 URL 用作“https://www.youtube-nocookie。com/embed/ajMpIN6BvQE”时,它停止显示推荐,但这里的挑战是我无法自动播放视频。它迫使用户明确点击播放按钮。下面是我使用的代码
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; public class WebViewActivity extends Activity { WebView webView; WebChromeClient.CustomViewCallback customViewCallback; @SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_webview); webView = findViewById(R.id.webView); webView.setWebViewClient(new WebViewClient()); ChromeClient chromeClient = new ChromeClient(); webView.setWebChromeClient(chromeClient); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setAllowFileAccess(true); webSettings.setAppCacheEnabled(true); // webView.loadUrl("https://www.youtube-nocookie.com/embed/ajMpIN6BvQE"); webView.loadUrl("https://www.youtube.com/watch?v=ajMpIN6BvQE"); webSettings.setMediaPlaybackRequiresUserGesture(false); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } }); } @Override public void onPause() { webView.onPause(); super.onPause(); } @Override public void onDestroy() { webView.destroy(); super.onDestroy(); } @Override public void onStop() { super.onStop(); } @Override protected void onRestart() { super.onRestart(); } public void onBackPressed() { webView.stopLoading(); finish(); } @Override protected void onStart() { super.onStart(); } public class ChromeClient extends WebChromeClient { protected FrameLayout mFullscreenContainer; private View mCustomView; private WebChromeClient.CustomViewCallback mCustomViewCallback; private int mOriginalOrientation; private int mOriginalSystemUiVisibility; ChromeClient() { } public Bitmap getDefaultVideoPoster() { if (mCustomView == null) { return null; } return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573); } @Override public void onHideCustomView() { ((FrameLayout) getWindow().getDecorView()).removeView(this.mCustomView); this.mCustomView = null; getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility); setRequestedOrientation(this.mOriginalOrientation); this.mCustomViewCallback.onCustomViewHidden(); this.mCustomViewCallback = null; } @Override public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback) { if (this.mCustomView != null) { onHideCustomView(); return; } this.mCustomView = paramView; this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility(); this.mOriginalOrientation = getRequestedOrientation(); this.mCustomViewCallback = paramCustomViewCallback; ((FrameLayout) getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); } }}
我能够通过 https://developers.google.com/youtube/iframe_api_reference 获得解决方案。添加了 Java 脚本代码并启用 Java 脚本与原生 android.
之间的交互