Home | History | Annotate | Download | only in actions
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This file performs actions on media elements.
      6 (function() {
      7   function playMedia(selector) {
      8     // Performs the "Play" action on media satisfying selector.
      9     var mediaElements = window.__findMediaElements(selector);
     10     for (var i = 0; i < mediaElements.length; i++) {
     11       console.log('Playing element: ' + mediaElements[i].src);
     12       play(mediaElements[i]);
     13     }
     14   }
     15 
     16   function play(element) {
     17     if (element instanceof HTMLMediaElement)
     18       playHTML5Element(element);
     19     else
     20       throw new Error('Can not play non HTML5 media elements.');
     21   }
     22 
     23   function playHTML5Element(element) {
     24     function logEventHappened(e) {
     25       element[e.type + '_completed'] = true;
     26     }
     27     function onError(e) {
     28       throw new Error('Error playing media :' + e.type);
     29     }
     30     element.addEventListener('playing', logEventHappened);
     31     element.addEventListener('ended', logEventHappened);
     32     element.addEventListener('error', onError);
     33     element.addEventListener('abort', onError);
     34 
     35     var willPlayEvent = document.createEvent('Event');
     36     willPlayEvent.initEvent('willPlay', false, false);
     37     element.dispatchEvent(willPlayEvent);
     38     element.play();
     39   }
     40 
     41   window.__playMedia = playMedia;
     42 })();
     43