web-audio-player
Version:
a cross-browser WebAudio player
49 lines (43 loc) • 1.44 kB
JavaScript
var lookup = require('browser-media-mime-type')
var audio
module.exports = isSrcPlayable
function isSrcPlayable (src) {
if (!src) throw new TypeError('src cannot be empty')
var type
if (typeof src.getAttribute === 'function') {
// <source> element
type = src.getAttribute('type')
} else if (typeof src === 'string') {
// 'foo.mp3' string
var ext = extension(src)
if (ext) type = lookup(ext)
} else {
// { src: 'foo.mp3', type: 'audio/mpeg; codecs..'}
type = src.type
}
// We have an unknown file extension or
// a <source> tag without an explicit type,
// just let the browser handle it!
if (!type) return true
// handle "no" edge case with super legacy browsers...
// https://groups.google.com/forum/#!topic/google-web-toolkit-contributors/a8Uy0bXq1Ho
if (!audio) audio = new window.Audio()
var canplay = audio.canPlayType(type).replace(/no/, '')
return Boolean(canplay)
}
module.exports.createError = createError
function createError (sources) {
// All sources are unplayable
var err = new Error('This browser does not support any of the following sources:\n ' +
sources.join(', ') + '\n' +
'Try using an array of OGG, MP3 and WAV.')
err.type = 'AUDIO_FORMAT'
return err
}
function extension (data) {
var extIdx = data.lastIndexOf('.')
if (extIdx <= 0 || extIdx === data.length - 1) {
return undefined
}
return data.substring(extIdx + 1)
}