OLD | NEW |
| (Empty) |
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 loopMedia(selector, loopCount) { | |
8 // Loops media playback `loopCount` times. | |
9 var mediaElements = window.__findMediaElements(selector); | |
10 for (var i = 0; i < mediaElements.length; i++) { | |
11 loop(mediaElements[i], loopCount); | |
12 } | |
13 } | |
14 | |
15 function loop(element, loopCount) { | |
16 if (element instanceof HTMLMediaElement) | |
17 loopHTML5Element(element, loopCount); | |
18 else | |
19 throw new Error('Can not play non HTML5 media elements.'); | |
20 } | |
21 | |
22 function loopHTML5Element(element, loopCount) { | |
23 window.__registerHTML5ErrorEvents(element); | |
24 element['loop_completed'] = false; | |
25 var currentLoop = 0; | |
26 var onLoop = function(e) { | |
27 ++currentLoop; | |
28 if (currentLoop == loopCount) { | |
29 element.pause(); | |
30 element.removeEventListener('seeked', onLoop); | |
31 element['loop_completed'] = true; | |
32 // Dispatch endLoopEvent to mark end of looping. | |
33 var endLoopEvent = document.createEvent('Event'); | |
34 endLoopEvent.initEvent('endLoop', false, false); | |
35 element.dispatchEvent(endLoopEvent); | |
36 } | |
37 }; | |
38 | |
39 element.addEventListener('seeked', onLoop); | |
40 element.loop = true; | |
41 | |
42 // Dispatch willLoopEvent to measure loop time. | |
43 var willLoopEvent = document.createEvent('Event'); | |
44 willLoopEvent.initEvent('willLoop', false, false); | |
45 willLoopEvent.loopCount = loopCount; | |
46 element.dispatchEvent(willLoopEvent); | |
47 // Reset HTML5 player to start playback from beginning. | |
48 element.load(); | |
49 element.play(); | |
50 } | |
51 | |
52 window.__loopMedia = loopMedia; | |
53 })(); | |
OLD | NEW |