【问题标题】:How do I fix this: app enables background playing of YouTube videos如何解决此问题:应用启用 YouTube 视频的后台播放
【发布时间】:2015-05-14 18:37:19
【问题描述】:

我制作了一个可以播放 YouTube 视频的 phonegap 应用。谷歌已将其从 Play 商店中撤下,因为“该应用支持后台播放 YouTube 视频。”

我不知道那是什么意思。

谁能帮我解决这个问题,让视频不会在后台播放?

谢谢。

【问题讨论】:

标签: cordova youtube


【解决方案1】:

我认为谷歌意味着你必须在应用程序处于后台模式时暂停 youtube 视频(例如触摸设备主页按钮)

我解决它的方法是注册“暂停”事件(当应用程序进入后台时调用)

document.addEventListener("pause", pause, false);
function pause (argument) {
  if (typeof document.app.player != "undefined") {
    document.app.player.pauseVideo();
  }

}

播放 youtube 时,我会保留播放器的参考:

        patt=/\/www\.youtube\.com\/watch\?v=(.*)/;
        m = patt.exec(url);
        if (m.length == 2) {
            url = "http://www.youtube.com/embed/"+ m[1] //+ "?autoplay=1"
            console.log("url = "+url)

        }

        YTid = 'yt_'+m[1];

        $("#MediaPPSVideo").html(
            "<div id='"+YTid+"' width=\"100%\"></div>"
        ).after(function() {
            document.app.player = new YT.Player(YTid, {
              height: $(window).height()/2,
              width: $(window).width()*0.95,
              videoId: m[1],
              events: {
                'onReady': onPlayerReady,
              }
            });
        })

这个丑陋但有效

【讨论】:

    【解决方案2】:

    将此添加到 config.xml 文件中,为我工作 Cordova 6.4.0。

    <preference name="KeepRunning" value="false" />
    

    在以下活动中进行更改 公共类 MainActivity 扩展 CordovaActivity

    @Override
    protected CordovaWebViewEngine makeWebViewEngine() {
        return new WebViewEngine(this, preferences);
    }
    
    
    static class WebViewEngine extends SystemWebViewEngine {
    
        public WebViewEngine(Context context, CordovaPreferences preferences) {
            super(context, preferences);
        }
    
    
        @Override
        public void setPaused(boolean value) {
            super.setPaused(value);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                return;
            } else if (value) {
                webView.onPause();
            } else {
                webView.onResume();
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这里可能有几件事:

      1. 我想知道 Google 是否真的意味着“后台播放”......或者它们真的意味着只是“一起玩”。他们可能会要求您将用户重定向到 YouTube 才能播放。 Google 可能希望 YouTube 成为唯一的 YouTube 视频播放器应用。

      2. 希望更有可能是 --- 您需要确保当您的应用程序退出/结束/暂停时,您还强制关闭/停止应用程序中的播放器。如果您使用的是 webView,即使您的应用不可见,它也可能会继续播放视频。

      【讨论】:

      • 我在这里没有看到任何答案。请添加为评论
      【解决方案4】:

      要在应用程序处于后台时停止播放 HTML 视频,您需要使用 config.xml 中的 KeepRunning 首选项暂停 WebView 计时器:

      <preference name="KeepRunning" value="false" />
      

      当你的 Activity 暂停时,也调用 WebView 的 onPause 方法。要使用 Cordova Android 4.0.0 执行此操作,您可以使用自定义 CordovaWebViewEngine 实现:

      public class MainActivity extends CordovaActivity {
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              super.init();
      
              // Set by <content src="index.html" /> in config.xml
              loadUrl(launchUrl);
          }
      
          @Override
          protected CordovaWebViewEngine makeWebViewEngine() {
              return new WebViewEngine(this, preferences);
          }
      
          /**
           * Custom Engine implementation so it can pass resume and pause events to WebView.
           * This is necessary to stop HTML5 video when app is put to background.
           */
          static class WebViewEngine extends SystemWebViewEngine {
      
              public WebViewEngine(Context context, CordovaPreferences preferences) {
                  super(context, preferences);
              }
      
              public WebViewEngine(SystemWebView webView) {
                  super(webView);
              }
      
              @Override
              public void setPaused(boolean value) {
                  super.setPaused(value);
                  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                      return;
                  } else if (value) {
                      webView.onPause();
                  } else {
                      webView.onResume();
                  }
              }
          }
      
      }
      

      【讨论】:

      • java 文件中的这种更改会在构建应用程序时产生各种错误。我们需要包含一个特殊的导入吗?
      • 没什么特别的,只要确保您运行的是 Cordova Android 4.0.0+ 并添加所有必需的导入:org.apache.cordova.engine.SystemWebVieworg.apache.cordova.engine.SystemWebViewEngineorg.apache.cordova.CordovaWebViewEngine 等。
      猜你喜欢
      • 2022-11-28
      • 2019-07-03
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2015-03-02
      • 2013-08-04
      • 2014-12-02
      • 1970-01-01
      相关资源
      最近更新 更多