canvasvideo.js
v1.0.3
Published
canvasvideo.js is a lightweight library to play inline video with sound on iPhone (Safari).
Readme
CanvasVideo.js
canvasvideo.js is a lightweight library to play video file in a canvas element with audio support. This is the best way to play inline video with sound on iPhone.
Presentation
We thought canvasvideo.js to help people to create immersive video experiences on mobile devices. This library draw video element on a canvas and sync it with the audio (from the video file or an audio file fallback).
Features
- plays inline video file (mp4) with audio on iPhone (directly in the browser)
- autoplays video without sound on iOS device (without touch event)
- Manages Audio by the webaudio API (with HTML5 Audio fallback)
- Auto unlock webaudio playback on the first touchend event.
- supports xhr
- supports playbackRate
- similar methods/events to video DOM element
Live demo
Featured project
Getting started
Installation
npm install canvasvideo.js --saveImport
You can import canvasvideo.js as a Browserify module.
var CanvasVideo = require('canvasvideo.js');or with a classic script tag.
<script type='text/javascript' src='js/canvasvideo.js'></script>Basic usage example
var video = new CanvasVideo('./videos/file.mp4', {audio:true, xhr:true, audioBuffer:false});
video.addEventListener('canplaythrough', onLoaded);
video.load();
function onLoaded(e)
{
document.body.appendChild(video.element);
if (!video.needTouch) video.play();
else document.body.addEventListener('touchend', playVideo);
}
function playVideo()
{
video.play();
}Autoplay video without sound on iOS
var options = {
xhr: false,
bufferTime: 6,
audio: false,
preload: true,
autoplay: true
}
var video = new CanvasVideo('./videos/file.mp4', options);
document.body.appendChild(video.element);Video with mp3 file
var options = {
xhr: false,
audioBuffer: true,
audio: './sound/file.mp3',
loop: true
}
var video = new CanvasVideo('./videos/file.mp4', options);
video.addEventListener('canplaythrough', onLoaded);
video.load ();
function onLoaded(e)
{
document.body.appendChild(video.element);
if (!video.needTouch) video.play();
else document.body.addEventListener('touchend', playVideo);
}
function playVideo()
{
video.play();
}Use canvasvideo.js as a video fallback on iPhone
<video id='myvideo' loop>
<source src='./videos/file.mp4' type='video/mp4'>
</video>
<script>
var video = document.getElementById('myvideo');
if (navigator.userAgent.toLowerCase().indexOf('iphone') >= 0)
{
document.body.removeChild(video);
video = new CanvasVideo(video, {xhr:true, audioBuffer:false});
document.body.appendChild(video.element);
}
video.addEventListener('canplaythrough', onLoaded);
video.load();
function onLoaded(e)
{
console.log(video.loop); // true
video.play();
}
</script>Documentation
Constructor
var video = new CanvasVideo(src, options);- src: This parameter could be:
Stringurl of a video file.Objectobject containing informations about the video file.{src:'file.mp4', type:'video/mp4'}
ArrayArray of strings or objects.['file.mp4', 'file.ogg'][{src:'file.mp4', type:'video/mp4'}, {src:'file.ogg', type:'video/ogg'}]
<video>a video element.
- options:
ObjectObject that could contain:- audio: This parameter could be:
Boolean(default:false) Set totrueto play audio channel of the video file.Stringurl of an audio file.
- audioBuffer:
Boolean(default:false) Set to false to use the webaudio API (fully preload audio file) or set to true to force HTML5 Audio (the audio file could be play without to be fully preload). - audioContext:
AudioContext(optional) the AudioContext to use. - autoplay:
Boolean(default:false) Set to true to start playing when it is loaded. - bufferTime:
Number(default:4.0) The number of seconds assigned to the buffer (only use if you don't use xhr). - canvas:
<canvas>(optional) the canvas to use as canvasvideo. - fps:
Number(default:24.0) Frame per seconds. - loop:
Boolean(default:false) Set totrueto automatically start over again when finished. - playbackRate:
Number(default:1.0) Set the default speed of the audio/video playback. - preload:
Boolean(default:false) Set to true to automatically load when canvasvideo is create. - volume:
Number(default:1.0) Set the default volume of the audio. - xhr:
Boolean(default:true) Set to true to fully preload video/audio file(s).- Recommanded if
audio:trueto not preload the video file twice (as video element and as audio element).
- Recommanded if
- audio: This parameter could be:
Methods
- canPlayType: Checks if the browser can play the specified audio/video type.
- destroy: Destroy properly the CanvasVideo element.
- load: Begins preload of video & audio.
- pause: Pauses playback of video & audio.
- play: Begins playback of video & audio.
Properties
- autoplay:
BooleanGet/set if the video should start playing as soon as it is loaded. - bufferLength:
NumberGet the number of seconds of data currently in the buffer. - bufferTime:
NumberGet/set the number of seconds assigned to the buffer. - currentSrc:
StringGet the URL of the current video. - currentTime:
NumberGet/set the current time of the canvasvideo. - duration:
NumberReturns the length of the current audio/video (in seconds). - element:
<canvas>Returns the canvas element use as canvasvideo. - fps:
NumberGet the video framerate. - height:
NumberGet/set the canvasvideo height. - id:
StringGet/set canvas element id. - loop:
BooleanGet/set the loop parameter. - muted:
BooleanSets or returns whether the video/audio is muted or not. - needTouchDevice:
Booleanreturns if the device require a touchend event to start playing. - paused:
BooleanReturns whether the audio/video is paused or not. - playbackRate:
NumberSets or returns the speed of the audio/video playback. - readyState:
NumberReturns the current ready state of the audio/video (only 0 or 4). - seeking:
BooleanReturns whether the user is currently seeking in the audio/video. - src:
String/Object/Array/<video>Returns the src of the canvasvideo. - useWebAudioAPI:
BooleanReturns true if canvasvideo is using the web audio API. - videoHeight:
NumberGet the original video height. - videoWidth:
NumberGet the original video width. - volume:
NumberGet/set the video audio volume. - width:
NumberGet/set the canvasvideo width. - xhr:
BooleanGet the xhr parameter.
Events
- canplay: Fires when the browser can start playing the video
- canplaythrough: Fires when the buffer is full.
- ended: Fires when the current playlist is ended.
- loadstart Fires when the browser has loaded the current frame of the audio/video.
- pause: Fires when the audio/video has been paused.
- play: Fires when the audio/video has been started or is no longer paused.
- playing: Fires when the audio/video is playing after having been paused or stopped for buffering.
- progress: Fires when the browser is downloading the audio/video.
- seeked: Fires when the user is finished moving/skipping to a new position in the audio/video.
- seeking: Fires when the user starts moving/skipping to a new position in the audio/video.
- timeupdate: Fires when the current playback position has changed.
- waiting: Fires when the video stops because it needs to buffer the next frame.
Browser Compatibility
Tested in the following browsers/OS:
- Mobile iOS 8.0+
Known issues
- Doesn't work on Android devices.
Releases
License
This content is released under the (http://opensource.org/licenses/MIT) MIT License.

