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

Changes audio rendering to WaveSurfer rendering #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var MediaElementWrapper = require('mediasource')
var path = require('path')
var streamToBlobURL = require('stream-to-blob-url')
var videostream = require('videostream')
var WaveSurfer = require('wavesurfer.js')

var VIDEOSTREAM_EXTS = [ '.mp4', '.m4v', '.m4a' ]

Expand Down Expand Up @@ -52,7 +53,8 @@ function append (file, rootElem, cb) {
}

renderMedia(file, function (tagName) {
if (tagName === 'video' || tagName === 'audio') return createMedia(tagName)
if (tagName === 'wave') return createWaveSurfer()
else if (tagName === 'video' || tagName === 'audio') return createMedia(tagName)
else return createElem(tagName)
}, function (err, elem) {
if (err && elem) elem.remove()
Expand All @@ -68,6 +70,15 @@ function append (file, rootElem, cb) {
return elem
}

function createWaveSurfer () {
return WaveSurfer.create({
container: rootElem,
backend: 'MediaElement',
waveColor: 'violet',
progressColor: 'purple'
})
}

function createElem (tagName) {
var elem = document.createElement(tagName)
rootElem.appendChild(elem)
Expand All @@ -84,7 +95,7 @@ function renderMedia (file, getElem, cb) {
if (MEDIASOURCE_EXTS.indexOf(extname) >= 0) {
renderMediaSource()
} else if (AUDIO_EXTS.indexOf(extname) >= 0) {
renderAudio()
renderWave()
} else if (IMAGE_EXTS.indexOf(extname) >= 0) {
renderImage()
} else if (IFRAME_EXTS.indexOf(extname) >= 0) {
Expand Down Expand Up @@ -166,18 +177,29 @@ function renderMedia (file, getElem, cb) {
}
}

function onReady () {
elem.params.container.querySelector('audio').controls = true
elem.play()
}

function onPlaying () {
elem.removeEventListener('playing', onPlaying)
cb(null, elem)
}

function renderAudio () {
elem = getElem('audio')
function onWavePlaying () {
elem.un('play', onWavePlaying)
cb(null, elem)
}

function renderWave () {
elem = getElem('wave')
getBlobURL(file, function (err, url) {
if (err) return fatalError(err)
elem.addEventListener('error', fatalError)
elem.addEventListener('playing', onPlaying)
elem.src = url
elem.on('error', fatalError)
elem.on('ready', onReady)
elem.on('play', onWavePlaying)
elem.load(url)
})
}

Expand All @@ -193,7 +215,6 @@ function renderMedia (file, getElem, cb) {

function renderIframe () {
elem = getElem('iframe')

getBlobURL(file, function (err, url) {
if (err) return fatalError(err)
elem.src = url
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"is-ascii": "^1.0.0",
"mediasource": "^2.1.0",
"stream-to-blob-url": "^2.0.0",
"videostream": "^2.3.0"
"videostream": "^2.3.0",
"wavesurfer.js": "^1.0.58"
},
"devDependencies": {
"brfs": "^1.4.1",
Expand Down