• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    window.__registerHTML5ErrorEvents(element);
25    window.__registerHTML5EventCompleted(element, 'playing');
26    window.__registerHTML5EventCompleted(element, 'ended');
27
28    var willPlayEvent = document.createEvent('Event');
29    willPlayEvent.initEvent('willPlay', false, false);
30    element.dispatchEvent(willPlayEvent);
31    element.play();
32  }
33
34  window.__playMedia = playMedia;
35})();
36