Home | History | Annotate | Download | only in common
      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.
      4 
      5 goog.provide('cvox.ChromeVoxHTMLMediaWidget');
      6 
      7 /**
      8  * @fileoverview Gives the user spoken feedback as they interact with the HTML5
      9  * media widgets (<video> and <audio>) + makes the widget keyboard accessible.
     10  *
     11  */
     12 
     13 /**
     14  * A class containing the information needed to speak
     15  * a media element to the user.
     16  *
     17  * @constructor
     18  * @param {Element} mediaElem The media widget element.
     19  * @param {cvox.TtsInterface} tts The TTS object from ChromeVox.
     20  */
     21 cvox.ChromeVoxHTMLMediaWidget = function(mediaElem, tts){
     22   var self = this;
     23   this.mediaElem_ = mediaElem;
     24   this.mediaTts_ = tts;
     25 
     26   this.keyListener_ = function(evt) {
     27     self.eventHandler_(evt);
     28   }
     29   this.blurListener_ = function(evt) {
     30     self.shutdown();
     31   }
     32 
     33   this.mediaElem_.addEventListener('keydown', this.keyListener_, false);
     34   this.mediaElem_.addEventListener('keyup', this.keyListener_, false);
     35   this.mediaElem_.addEventListener('blur', this.blurListener_, false);
     36 };
     37 
     38 /**
     39  * Removes the key listeners for the media widget.
     40  */
     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);
     45 };
     46 
     47 cvox.ChromeVoxHTMLMediaWidget.prototype.jumpToTime_ = function(targetTime) {
     48   if (targetTime < 0) {
     49     targetTime = 0;
     50   }
     51   if (targetTime > this.mediaElem_.duration) {
     52     targetTime = this.mediaElem_.duration;
     53   }
     54   this.mediaElem_.currentTime = targetTime;
     55 };
     56 
     57 cvox.ChromeVoxHTMLMediaWidget.prototype.setVolume_ = function(targetVolume) {
     58   if (targetVolume < 0) {
     59     targetVolume = 0;
     60   }
     61   if (targetVolume > 1.0) {
     62     targetVolume = 1.0;
     63   }
     64   this.mediaElem_.volume = targetVolume;
     65 };
     66 
     67 /**
     68  * Adds basic keyboard handlers to the media widget.
     69  */
     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();
     76       } else {
     77         this.mediaElem_.pause();
     78       }
     79     } else if (evt.keyCode == 39) { // Right - FF
     80       this.jumpToTime_(
     81           this.mediaElem_.currentTime + (this.mediaElem_.duration/10));
     82     } else if (evt.keyCode == 37) { // Left - REW
     83       this.jumpToTime_(
     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);
     89     }
     90   }
     91 };
     92