Source: speech-to-text/file-player.js

'use strict';

var getContentTypeFromHeader = require('./content-type');

/**
 * Plays audio from a URL
 * Compatible with Mobile Safari if triggered in direct response to a user interaction (e.g. click)
 * @param {String} url
 * @constructor
 */
function UrlPlayer(url) {
  var audio = this.audio = new Audio();
  audio.src = url;
  audio.play();
  /**
   * Stops the audio
   */
  this.stop = function stop() {
    audio.pause();
    audio.currentTime = 0;
  };
}

/**
 * Plays audio from File/Blob instances
 * @param {File|Blob} file
 * @param {String} contentType
 * @constructor
 */
function FilePlayer(file, contentType) {
  var audio = this.audio = new Audio();
  if (audio.canPlayType(contentType)) {
    audio.src = URL.createObjectURL(new Blob([file], { type: contentType }));
    audio.play();
  } else {
    // if we emit an error, it prevents the promise from returning the actual result
    // however, most browsers do not support flac, so this is a reasonably scenario
    var err = new Error('Current browser is unable to play back ' + contentType);
    err.name = FilePlayer.ERROR_UNSUPPORTED_FORMAT;
    err.contentType = contentType;
    throw err;
  }
  /**
   * Stops the audio
   */
  this.stop = function stop() {
    audio.pause();
    audio.currentTime = 0;
  };
}
FilePlayer.ERROR_UNSUPPORTED_FORMAT = 'UNSUPPORTED_FORMAT';

/**
 * Reads the first few bytes of a binary file and resolves to the content-type if recognized & supported
 * @param {File|Blob} file
 * @return {Promise}
 */
function getContentTypeFromFile(file) {
  return new Promise(function(resolve, reject) {
    var blobToText = new Blob([file]).slice(0, 4);
    var r = new FileReader();
    r.readAsText(blobToText);
    r.onload = function() {
      var ct = getContentTypeFromHeader(r.result);
      if (ct) {
        resolve(ct);
      } else {
        var err = new Error('Unable to determine content type from file header; only wav, flac, and ogg/opus are supported.');
        err.name = FilePlayer.ERROR_UNSUPPORTED_FORMAT;
        reject(err);
      }
    };
  });
}

/**
 * Determines the file's content-type and then resolves to a FilePlayer instance
 * @param {File|Blob|String} file - binary data or URL of audio file (binary data playback may not work on mobile Safari)
 * @return {Promise.<FilePlayer>}
 */
function playFile(file) {
  if (typeof file === 'string') {
    return Promise.resolve(new UrlPlayer(file));
  }
  return getContentTypeFromFile(file).then(function(contentType) {
    return new FilePlayer(file, contentType);
  });
}

module.exports = FilePlayer;
module.exports.getContentType = getContentTypeFromFile;
module.exports.playFile = playFile;