<commit_before>// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

var Playback = require('playback')
var Events = require('events')

class HTML5Audio extends Playback {
  get name() { return 'html5_audio' }
  get tagName() { return 'audio' }
  get events() {
    return {
      'timeupdate': 'timeUpdated',
      'ended': 'ended',
      'canplaythrough': 'bufferFull'
    }
  }

  constructor(params) {
    super(params)
    this.el.src = params.src
    this.settings = {
      left: ['playpause', 'position', 'duration'],
      right: ['fullscreen', 'volume'],
      default: ['seekbar']
    }
    this.render()
    params.autoPlay && this.play()
  }

  bindEvents() {
    this.listenTo(this.container, Events.CONTAINER_PLAY, this.play)
    this.listenTo(this.container, Events.CONTAINER_PAUSE, this.pause)
    this.listenTo(this.container, Events.CONTAINER_SEEK, this.seek)
    this.listenTo(this.container, Events.CONTAINER_VOLUME, this.volume)
    this.listenTo(this.container, Events.CONTAINER_STOP, this.stop)
  }

  getPlaybackType() {
    return "aod"
  }

  play() {
    this.el.play()
    this.trigger(Events.PLAYBACK_PLAY);
  }

  pause() {
    this.el.pause()
  }

  stop() {
    this.pause()
    this.el.currentTime = 0
  }

  volume(value) {
    this.el.volume = value / 100
  }

  mute() {
    this.el.volume = 0
  }

  unmute() {
    this.el.volume = 1
  }

  isMuted() {
    return !!this.el.volume
  }

  ended() {
    this.trigger(Events.CONTAINER_TIMEUPDATE, 0)
  }

  seek(seekBarValue) {
    var time = this.el.duration * (seekBarValue / 100)
    this.el.currentTime = time
  }

  getCurrentTime() {
    return this.el.currentTime
  }

  getDuration() {
    return this.el.duration
  }

  isPlaying() {
    return !this.el.paused && !this.el.ended
  }

  timeUpdated() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
  }

  bufferFull() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
    this.trigger(Events.PLAYBACK_BUFFERFULL)
  }

  render() {
    this.trigger(Events.PLAYBACK_READY, this.name)
    return this
  }
 }

HTML5Audio.canPlay = function(resource) {
  return !!resource.match(/(.*).mp3/)<commit_msg>html5_audio: use canPlayType on canPlay method (close #339)<commit_after>// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

var Playback = require('playback')
var Events = require('events')
var _ = require('underscore')

class HTML5Audio extends Playback {
  get name() { return 'html5_audio' }
  get tagName() { return 'audio' }
  get events() {
    return {
      'timeupdate': 'timeUpdated',
      'ended': 'ended',
      'canplaythrough': 'bufferFull'
    }
  }

  constructor(params) {
    super(params)
    this.el.src = params.src
    this.settings = {
      left: ['playpause', 'position', 'duration'],
      right: ['fullscreen', 'volume'],
      default: ['seekbar']
    }
    this.render()
    params.autoPlay && this.play()
  }

  bindEvents() {
    this.listenTo(this.container, Events.CONTAINER_PLAY, this.play)
    this.listenTo(this.container, Events.CONTAINER_PAUSE, this.pause)
    this.listenTo(this.container, Events.CONTAINER_SEEK, this.seek)
    this.listenTo(this.container, Events.CONTAINER_VOLUME, this.volume)
    this.listenTo(this.container, Events.CONTAINER_STOP, this.stop)
  }

  getPlaybackType() {
    return "aod"
  }

  play() {
    this.el.play()
    this.trigger(Events.PLAYBACK_PLAY);
  }

  pause() {
    this.el.pause()
  }

  stop() {
    this.pause()
    this.el.currentTime = 0
  }

  volume(value) {
    this.el.volume = value / 100
  }

  mute() {
    this.el.volume = 0
  }

  unmute() {
    this.el.volume = 1
  }

  isMuted() {
    return !!this.el.volume
  }

  ended() {
    this.trigger(Events.CONTAINER_TIMEUPDATE, 0)
  }

  seek(seekBarValue) {
    var time = this.el.duration * (seekBarValue / 100)
    this.el.currentTime = time
  }

  getCurrentTime() {
    return this.el.currentTime
  }

  getDuration() {
    return this.el.duration
  }

  isPlaying() {
    return !this.el.paused && !this.el.ended
  }

  timeUpdated() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
  }

  bufferFull() {
    this.trigger(Events.PLAYBACK_TIMEUPDATE, this.el.currentTime, this.el.duration, this.name)
    this.trigger(Events.PLAYBACK_BUFFERFULL)
  }

  render() {
    this.trigger(Events.PLAYBACK_READY, this.name)
    return this
  }
 }

HTML5Audio.canPlay = function(resource) {
  var mimetypes = {
    'wav': ['audio/wav'],
    'mp3': ['audio/mp3', 'audio/mpeg;codecs="mp3"'],
    'aac': ['audio/mp4;codecs="mp4a.40.5"']
  }
  var extension = resource.split('?')[0].match(/.*\.(.*)$/)[1]

  if (_.has(mimetypes, extension)) {
    var a = document.createElement('audio')
    return !!_.find(mimetypes[extension], function (ext) { return !!a.canPlayType(ext).replace(/no/, '') })
  }
  return false
{'fraction_non_alphanumeric': 0.1483633546387759, 'fraction_numerical': 0.009490606236684098, 'mean_word_length': 3.043852779953015, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7378748', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1792, 'n_words': 340}