Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support audio buffer as source #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 24 additions & 13 deletions lib/buffer-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,34 @@ function createBufferSource (src, opt) {
return emitter

function startLoad (src) {
xhrAudio(audioContext, src, function audioDecoded (err, decoded) {
if (err) return emitter.emit('error', err)
buffer = decoded // store for later use
if (bufferNode) {
// if play() was called early
bufferNode.buffer = buffer
}
duration = buffer.duration
node.buffer = buffer
emitter.emit('load')
}, function audioProgress (amount, total) {
emitter.emit('progress', amount, total)
}, function audioDecoding () {
if (typeof src.duration === 'number')
return audioDecoded(null, src);

xhrAudio(
audioContext,
src,
audioDecoded,
function audioProgress (amount, total)
{
emitter.emit('progress', amount, total)
}, function audioDecoding ()
{
emitter.emit('decoding')
})
}

function audioDecoded (err, decoded) {
if (err) return emitter.emit('error', err)
buffer = decoded // store for later use
if (bufferNode) {
// if play() was called early
bufferNode.buffer = buffer
}
duration = buffer.duration
node.buffer = buffer
emitter.emit('load')
}

function ended () {
emitter.emit('end')
playing = false
Expand Down
4 changes: 4 additions & 0 deletions lib/can-play-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var audio
module.exports = isSrcPlayable
function isSrcPlayable (src) {
if (!src) throw new TypeError('src cannot be empty')

if (typeof src.duration === 'number')
return Boolean(true);

var type
if (typeof src.getAttribute === 'function') {
// <source> element
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-audio-player",
"version": "1.3.1",
"version": "1.3.2",
"description": "a cross-browser WebAudio player",
"main": "index.js",
"license": "MIT",
Expand Down