gpt4 book ai didi

android - 在Fire TV中播放youtube视频时出现问题

转载 作者:行者123 更新时间:2023-12-03 06:05:44 25 4
gpt4 key购买 nike

我正在开发一个从后端获取youtube视频ID的应用,并且该应用应自动播放队列中的视频,而无需用户干预。但这是我面对不同方法所面临的挑战。

  • 使用Youtube API:-FireTV不支持此功能,并且我的应用未在亚马逊商店中获得批准。
  • 使用Webview:-
  • 当我将视频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实现解决方案。添加了Javascript代码,并启用了Java脚本与Native android之间的交互。

    关于android - 在Fire TV中播放youtube视频时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64028157/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com