【问题标题】:Pause a (html5) youtube video on an IOS device在 IOS 设备上暂停 (html5) youtube 视频
【发布时间】:2013-03-06 22:53:15
【问题描述】:

我正在尝试在已加载 youtube.com 的 IOS UIview 中使用 MY OWN 按钮暂停视频。我一直在尝试与 youtube api 进行交互,但没有运气。

我正在考虑注入我自己的代码来与播放器交互(swfobject 对象?是吗?)。我试图连接到播放器/swfobject,但没有成功。 再次澄清一下,我不想将自己的视频嵌入到新的网络视图中。(我知道该怎么做。一个很酷的方法是使用Tubeplayer plugin)。

所以我想做的是:

  1. 注入暂停 JavaScript 函数,我将从我的 移动应用。
  2. 然后该函数将执行以下操作:

    var theSwfobject = window.swfobject; //**Find the youtube video object for reuse.**
    theSwfobject.PauseVideo();
    

所以简短的问题是:如何在 youtube.com 上找到并重用 youtube 视频对象(这样我就可以注入一个 pause() 函数来从 IOS 调用以暂停视频) ?

【问题讨论】:

  • 你确定这是一个 HTML5 视频吗? Swf 似乎表示闪存。查看 youtube,视频在我的浏览器中加载为 flash。他们可能对 HTML5 进行了渐进式增强,但我不相信,因为我的浏览器支持 HTML5 视频。我想您正在寻找如何暂停 Flash 视频。
  • 如果确实是 HTML5 视频,则应该是 DOM 中的
  • 它可能根本不是 HTML5 视频。我使用的 iOS 5.1 版本仍然有“覆盖 Flash 视频,因为它们很危险并在苹果播放器中显示”。现在在更新的 6.0 及更高版本中,苹果仍然禁止 flash 但不显示视频,因此需要 hmtl5 视频。新的 swf 2.0 支持这一点。它找出支持的格式(默认为闪存)并写出播放器。我期待html5,但那可能是我错了。现在我正在更新 iOS,如果可行,我会知道你是否正确。
  • 好的,我更新了 iOS,现在我得到了一个
  • 太好了,现在看起来像 HTML5 视频。当你说“这就是我得到的

标签: javascript ios html youtube swfobject


【解决方案1】:

根据您的最新评论,我希望在 JavaScript 中做一些事情。这是我将设置的一个小 API 来控制此实例中的视频元素。由于视频元素似乎没有id,所以使用getElementsByTagName:

var myVideoController = {};

myVideoController = (function() {

  "use strict";      

  var muted = false;

  var module = {

    //Grabs video element by tag name and assumes there would only be one if it exists 
    getVideoElement : function() {
      var videoElements = document.getElementsByTagName('video');
      var videoElement = null;

      if(videoElements[0]) {
        videoElement = videoElements[0];
      }

      return videoElement;
    },

    /** 
     * Wrapper to make interacting with html5 video element functions easier.
     * @param functionName - name of function to invoke on the video element
     * @params - any additional parameters will be fed as arguments to the functionName function
     */
    callVideoFunction : function(functionName) {
      var videoElement = module.getVideoElement();
      var functionArguments = [];

      if(videoElement !== null) {
        functionArguments = module.getSubArguments(arguments, 1);

        if(functionArguments.length > 0) {
          videoElement[functionName](functionArguments);
        } else {
          videoElement[functionName]();
        }
      }
    },

    setVideoProperty : function(propertyName, propertyValue) {
      var videoElement = module.getVideoElement();

      if(videoElement !== null) {
        videoElement[propertyName] = propertyValue;
      }
    },

    /* Helper method to grab array of function arguments for callVideoFunction
       since the arguments object in functions looks like an array but isn't
       so .shift() is not defined */
    getSubArguments : function (args, indexFrom) {

      var subArguments = [];

      for(var i = indexFrom; i < args.length; i++) {
        subArguments.push(args[i]);
      }

      return subArguments;
    },

    //Pause the video
    pauseVideo : function() {
      module.callVideoFunction('pause');
    },

    //Play the video
    playVideo : function() {
      module.callVideoFunction('play');
    },

    //Mute/Unmute video
    flipVideoMute : function() {
      muted = !muted;
      module.setVideoProperty('muted', muted);
    }
  };

  return module;

})();

我在http://www.w3.org/2010/05/video/mediaevents.html 对其进行了测试,其中 w3 设置了一个 HTML5 视频,其中包含有关 api 使用情况的反馈。我将上面的代码复制到 javascript 控制台并运行如下命令:

//Start video
myVideoController.playVideo();

//Pause video
myVideoController.pauseVideo();

//Restart video
myVideoController.playVideo();

//Mute video
myVideoController.flipVideoMute();

//Unmute video
myVideoController.flipVideoMute();

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多