在IOS设备上暂停(html5)youtube视频

时间:2013-03-06 22:53:15

标签: javascript ios html5 youtube swfobject

我正在尝试在加载 youtube.com 的IOS UIview中使用我自己按钮暂停视频。我一直试图与youtube api互动而没有运气。

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

所以我想做的是:

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

    var theSwfobject = window.swfobject; //**Find the youtube video object for reuse.**
    theSwfobject.PauseVideo();
    
  3. 所以简短的问题是:如何在youtube.com上找到并重复使用youtube视频对象(这样我可以注入一个pause()函数来从IOS调用暂停视频) ?

1 个答案:

答案 0 :(得分:2)

根据您的最新评论,我希望能用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();