Add audio and video players

Basic implementation using html5 audio/video tags and data URIs.
This commit is contained in:
lilia 2015-03-02 11:49:23 -08:00
parent aa659877be
commit ddc0ed1b9a
3 changed files with 54 additions and 8 deletions

View file

@ -16,9 +16,42 @@
(function () {
'use strict';
var ImageView = Backbone.View.extend({
tagName: 'img',
render: function(dataUrl) {
this.$el.attr('src', dataUrl);
return this;
}
});
var AudioView = Backbone.View.extend({
tagName: 'audio',
initialize: function() {
this.$el.attr('controls', '');
},
render: function(dataUrl) {
this.$el.attr('src', dataUrl);
return this;
}
});
var VideoView = Backbone.View.extend({
tagName: 'video',
initialize: function() {
this.$el.attr('controls', '');
},
render: function(dataUrl, contentType) {
var $el = $('<source>');
$el.attr('src', dataUrl);
$el.attr('type', contentType);
this.$el.append($el);
return this;
}
});
Whisper.AttachmentView = Backbone.View.extend({
tagName: "img",
encode: function () {
className: 'attachment',
encodeAsDataUrl: function () {
return new Promise(function(resolve, reject) {
var blob = new Blob([this.model.data], { type: this.model.contentType });
var FR = new FileReader();
@ -30,8 +63,17 @@
}.bind(this));
},
render: function() {
this.encode().then(function(base64) {
this.$el.attr('src', base64);
var view;
switch(this.model.contentType.split('/')[0]) {
case 'image': view = new ImageView(); break;
case 'audio': view = new AudioView(); break;
case 'video': view = new VideoView(); break;
default:
throw 'Unsupported attachment type';
}
this.encodeAsDataUrl().then(function(base64) {
view.render(base64, this.model.contentType);
view.$el.appendTo(this.$el);
this.$el.trigger('update');
}.bind(this));
return this;