1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)goog.provide('cvox.ChromeVoxHTMLMediaWidget');
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/**
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * @fileoverview Gives the user spoken feedback as they interact with the HTML5
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * media widgets (<video> and <audio>) + makes the widget keyboard accessible.
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/**
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * A class containing the information needed to speak
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * a media element to the user.
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * @constructor
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * @param {Element} mediaElem The media widget element.
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * @param {cvox.TtsInterface} tts The TTS object from ChromeVox.
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cvox.ChromeVoxHTMLMediaWidget = function(mediaElem, tts){
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  var self = this;
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_ = mediaElem;
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaTts_ = tts;
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.keyListener_ = function(evt) {
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.eventHandler_(evt);
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.blurListener_ = function(evt) {
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    self.shutdown();
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.addEventListener('keydown', this.keyListener_, false);
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.addEventListener('keyup', this.keyListener_, false);
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.addEventListener('blur', this.blurListener_, false);
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/**
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Removes the key listeners for the media widget.
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cvox.ChromeVoxHTMLMediaWidget.prototype.shutdown = function() {
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.removeEventListener('blur', this.blurListener_, false);
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.removeEventListener('keydown', this.keyListener_, false);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.removeEventListener('keyup', this.keyListener_, false);
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cvox.ChromeVoxHTMLMediaWidget.prototype.jumpToTime_ = function(targetTime) {
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (targetTime < 0) {
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    targetTime = 0;
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (targetTime > this.mediaElem_.duration) {
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    targetTime = this.mediaElem_.duration;
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.currentTime = targetTime;
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cvox.ChromeVoxHTMLMediaWidget.prototype.setVolume_ = function(targetVolume) {
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (targetVolume < 0) {
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    targetVolume = 0;
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (targetVolume > 1.0) {
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    targetVolume = 1.0;
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  this.mediaElem_.volume = targetVolume;
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/**
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Adds basic keyboard handlers to the media widget.
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)cvox.ChromeVoxHTMLMediaWidget.prototype.eventHandler_ = function(evt) {
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (evt.type == 'keydown') {
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Space/Enter for play/pause toggle.
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if ((evt.keyCode == 13) || (evt.keyCode == 32)) {
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (this.mediaElem_.paused){
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        this.mediaElem_.play();
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      } else {
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        this.mediaElem_.pause();
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      }
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else if (evt.keyCode == 39) { // Right - FF
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      this.jumpToTime_(
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          this.mediaElem_.currentTime + (this.mediaElem_.duration/10));
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else if (evt.keyCode == 37) { // Left - REW
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      this.jumpToTime_(
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          this.mediaElem_.currentTime - (this.mediaElem_.duration/10));
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else if (evt.keyCode == 38) { // Up - Vol. Up
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      this.setVolume_(this.mediaElem_.volume + .1);
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else if (evt.keyCode == 40) { // Down - Vol. Down
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      this.setVolume_(this.mediaElem_.volume - .1);
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
92