1 // Copyright 2014 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.
5 goog
.provide('cvox.ChromeVoxHTMLMediaWidget');
8 * @fileoverview Gives the user spoken feedback as they interact with the HTML5
9 * media widgets (<video> and <audio>) + makes the widget keyboard accessible.
14 * A class containing the information needed to speak
15 * a media element to the user.
18 * @param {Element} mediaElem The media widget element.
19 * @param {cvox.TtsInterface} tts The TTS object from ChromeVox.
21 cvox
.ChromeVoxHTMLMediaWidget = function(mediaElem
, tts
){
23 this.mediaElem_
= mediaElem
;
26 this.keyListener_ = function(evt
) {
27 self
.eventHandler_(evt
);
29 this.blurListener_ = function(evt
) {
33 this.mediaElem_
.addEventListener('keydown', this.keyListener_
, false);
34 this.mediaElem_
.addEventListener('keyup', this.keyListener_
, false);
35 this.mediaElem_
.addEventListener('blur', this.blurListener_
, false);
39 * Removes the key listeners for the media widget.
41 cvox
.ChromeVoxHTMLMediaWidget
.prototype.shutdown = function() {
42 this.mediaElem_
.removeEventListener('blur', this.blurListener_
, false);
43 this.mediaElem_
.removeEventListener('keydown', this.keyListener_
, false);
44 this.mediaElem_
.removeEventListener('keyup', this.keyListener_
, false);
47 cvox
.ChromeVoxHTMLMediaWidget
.prototype.jumpToTime_ = function(targetTime
) {
51 if (targetTime
> this.mediaElem_
.duration
) {
52 targetTime
= this.mediaElem_
.duration
;
54 this.mediaElem_
.currentTime
= targetTime
;
57 cvox
.ChromeVoxHTMLMediaWidget
.prototype.setVolume_ = function(targetVolume
) {
58 if (targetVolume
< 0) {
61 if (targetVolume
> 1.0) {
64 this.mediaElem_
.volume
= targetVolume
;
68 * Adds basic keyboard handlers to the media widget.
70 cvox
.ChromeVoxHTMLMediaWidget
.prototype.eventHandler_ = function(evt
) {
71 if (evt
.type
== 'keydown') {
72 // Space/Enter for play/pause toggle.
73 if ((evt
.keyCode
== 13) || (evt
.keyCode
== 32)) {
74 if (this.mediaElem_
.paused
){
75 this.mediaElem_
.play();
77 this.mediaElem_
.pause();
79 } else if (evt
.keyCode
== 39) { // Right - FF
81 this.mediaElem_
.currentTime
+ (this.mediaElem_
.duration
/10));
82 } else if (evt
.keyCode
== 37) { // Left - REW
84 this.mediaElem_
.currentTime
- (this.mediaElem_
.duration
/10));
85 } else if (evt
.keyCode
== 38) { // Up - Vol. Up
86 this.setVolume_(this.mediaElem_
.volume
+ .1);
87 } else if (evt
.keyCode
== 40) { // Down - Vol. Down
88 this.setVolume_(this.mediaElem_
.volume
- .1);