Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function CordovaStorage(){
- var that = this;
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
- that.fs = fileSystem;
- }, fail);
- }
- CordovaStorage.prototype = {
- store: function(fileData, callback){
- var fileContent = Base64Binary.decodeArrayBuffer(fileData.content),
- fileName = [fileData.crc, fileData.fileType].join(".").replace(/\//g, "-");
- this.fs.root.getFile(fileName, {create: true, exclusive: false}, function(fileEntry){
- fileEntry.createWriter(function(fileWritter){
- fileWritter.onwriteend = function(){ callback(); }
- fileWritter.write(fileContent);
- }, fail);
- }, fail);
- },
- get: function (crc, fileType, callback) {
- var fileName = [crc, fileType].join(".").replace(/\//g, "-");
- this.fs.root.getFile(fileName, null, function(fileEntry) {
- callback(fileEntry.toURL())
- }, function(){console.log(arguments)})
- }
- }
- function DefaultStorage(){
- this.fileStore = db.fileStore();
- }
- DefaultStorage.prototype = {
- store: function(fileData, callback){
- var that = this
- that.fileStore.put(fileData).done(function(){
- callback();
- }).fail(function(){
- callback();
- });
- },
- contentProcessor: function(content){
- return content;
- },
- get: function(crc, fileType, callback){
- var that = this,
- prefixes = {
- "mp3": "data:audio/mp3;base64,",
- "ogg": "data:audio/ogg;base64,",
- "jpg": "data:image/jpeg;base64,",
- "jpeg": "data:image/jpeg;base64,",
- "png": "data:image/png;base64,",
- "gif": "data:image/gif;base64,",
- };
- this.fileStore.get(crc).done(function(result){
- var source = prefixes[fileType] + that.contentProcessor(result.content);
- callback(source);
- });
- }
- }
- function CordovaPlayer(){
- var player = this;
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
- player.fs = fileSystem;
- }, fail);
- }
- CordovaPlayer.prototype = {
- playQuestion: function(question, prefix){
- var player = this;
- fileName = question[prefix + "_mp3_crc"] + ".mp3";
- app.storage.get(question[prefix+"_mp3_crc"], "mp3", function(fileSource){
- player.cordovaMedia = new Media(fileSource);
- player.cordovaMedia.play();
- });
- },
- play: function(){
- this.cordovaMedia && this.cordovaMedia.play();
- },
- pause: function(){
- this.cordovaMedia && this.cordovaMedia.pause();
- },
- getAudioType: function(){
- return "mp3";
- }
- }
- function DefaultPlayer(){
- var player = this;
- this.media = {
- mp3: "data:audio/mp3;base64,SUQzAwAA...",
- ogg: "data:audio/ogg;base64,T2dnUwACAAAAAAAAAADLf+..."
- }
- this.createAudio();
- document.body.addEventListener("click", player.clickHandler = function(){
- player.play();
- document.body.removeEventListener("click", player.clickHandler);
- });
- }
- DefaultPlayer.prototype = {
- playQuestion: function(question, prefix) {
- var source_loaders = [], mp3Source, oggSource,
- player = this;
- source_loaders.push(function(next){
- app.storage.get(question[prefix+"_mp3_crc"], "mp3", function(source){
- mp3Source = source;
- next();
- });
- });
- source_loaders.push(function(next){
- app.storage.get(question[prefix+"_ogg_crc"], "ogg", function(source){
- oggSource = source;
- next();
- });
- });
- async.parallel(source_loaders, function(){
- app.player.load({
- mp3: mp3Source,
- ogg: oggSource
- }, function(){
- player.play();
- });
- });
- },
- createAudio: function(){
- var sources = [];
- sources.push(dom("source", {"type": "audio/mp3", src: this.media.mp3}));
- sources.push(dom("source", {"type": "audio/ogg", src: this.media.ogg}));
- this.audio = dom("audio", sources);
- },
- load: function(media, callback){
- this.media = media;
- this.audio.innerHTML = '';
- this.audio.appendChild(dom("source", {
- "type": "audio/ogg",
- src: this.media.ogg
- }));
- this.audio.appendChild(dom("source", {
- "type": "audio/mp3",
- src: this.media.mp3
- }));
- this.audio.load();
- setTimeout(function(){
- callback && callback();
- }, 100);
- },
- play: function(){
- this.audio.play();
- },
- pause: function(){
- this.audio && this.audio.pause();
- },
- getAudioType: function(){
- return "all";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement