Sturla
Sturla

Reputation: 3626

Pause a (html5) youtube video on an IOS device

I'm trying to pause videos with MY OWN buttons in a IOS UIview that has youtube.com loaded. I have been trying to interact with the youtube api without luck.

I'm looking at injecting my own code to interact with the player (swfobject object? Is it?). I have tried to hook up to the player/swfobject with no success. And just to clarify again I don't want embed the videos my self in a fresh webview. (I know how to do that. A cool way to do that is to use Tubeplayer plugin).

So what I would like to do is this:

  1. Inject pause JavaScript functions that I will call from my mobile app.
  2. The function will then do something like this:

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

So the short question is: How can I find and reuse the youtube video object on youtube.com (so that I can inject a pause() function to call from IOS to pause the video)?

Upvotes: 1

Views: 1299

Answers (1)

Rich
Rich

Reputation: 2885

Based on your latest comment I would expect to do something in JavaScript. Here is a little API I would setup to control the video element in this instance. Since the video element doesn't seem to have an id, used 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;

})();

I tested it at http://www.w3.org/2010/05/video/mediaevents.html where w3 has set up an HTML5 video with feedback on the api usage. I copied the above code into the javascript console and ran commands as follows:

//Start video
myVideoController.playVideo();

//Pause video
myVideoController.pauseVideo();

//Restart video
myVideoController.playVideo();

//Mute video
myVideoController.flipVideoMute();

//Unmute video
myVideoController.flipVideoMute();

Upvotes: 2

Related Questions