Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function CordovaStorage(){
  2.         var that = this;
  3.  
  4.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
  5.             that.fs = fileSystem;
  6.         }, fail);
  7.     }
  8.  
  9.     CordovaStorage.prototype = {
  10.         store: function(fileData, callback){
  11.             var fileContent = Base64Binary.decodeArrayBuffer(fileData.content),
  12.                 fileName = [fileData.crc, fileData.fileType].join(".").replace(/\//g, "-");
  13.  
  14.             this.fs.root.getFile(fileName, {create: true, exclusive: false}, function(fileEntry){
  15.                 fileEntry.createWriter(function(fileWritter){
  16.                     fileWritter.onwriteend = function(){ callback(); }
  17.                     fileWritter.write(fileContent);
  18.                 }, fail);
  19.             }, fail);
  20.         },
  21.         get: function (crc, fileType, callback) {
  22.             var fileName = [crc, fileType].join(".").replace(/\//g, "-");
  23.             this.fs.root.getFile(fileName, null, function(fileEntry) {
  24.                 callback(fileEntry.toURL())
  25.             }, function(){console.log(arguments)})
  26.         }
  27.     }
  28.  
  29.     function DefaultStorage(){
  30.         this.fileStore = db.fileStore();
  31.     }
  32.  
  33.     DefaultStorage.prototype = {
  34.         store: function(fileData, callback){
  35.             var that = this
  36.  
  37.             that.fileStore.put(fileData).done(function(){
  38.                 callback();
  39.             }).fail(function(){
  40.                 callback();
  41.             });
  42.         },
  43.         contentProcessor: function(content){
  44.             return content;
  45.         },
  46.         get: function(crc, fileType, callback){
  47.             var that = this,
  48.                 prefixes = {
  49.                     "mp3": "data:audio/mp3;base64,",
  50.                     "ogg": "data:audio/ogg;base64,",
  51.                     "jpg": "data:image/jpeg;base64,",
  52.                     "jpeg": "data:image/jpeg;base64,",
  53.                     "png": "data:image/png;base64,",
  54.                     "gif": "data:image/gif;base64,",
  55.                 };
  56.  
  57.             this.fileStore.get(crc).done(function(result){
  58.                 var source = prefixes[fileType] + that.contentProcessor(result.content);
  59.                 callback(source);
  60.             });
  61.         }
  62.     }
  63.  
  64.     function CordovaPlayer(){
  65.         var player = this;
  66.  
  67.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
  68.             player.fs = fileSystem;
  69.         }, fail);
  70.     }
  71.  
  72.     CordovaPlayer.prototype = {
  73.         playQuestion: function(question, prefix){
  74.             var player = this;
  75.             fileName = question[prefix + "_mp3_crc"] + ".mp3";
  76.  
  77.             app.storage.get(question[prefix+"_mp3_crc"], "mp3", function(fileSource){
  78.                 player.cordovaMedia = new Media(fileSource);
  79.                 player.cordovaMedia.play();
  80.             });
  81.         },
  82.         play: function(){
  83.             this.cordovaMedia && this.cordovaMedia.play();
  84.         },
  85.         pause: function(){
  86.             this.cordovaMedia && this.cordovaMedia.pause();
  87.         },
  88.         getAudioType: function(){
  89.             return "mp3";
  90.         }
  91.     }
  92.  
  93.     function DefaultPlayer(){
  94.         var player = this;
  95.         this.media = {
  96.             mp3: "data:audio/mp3;base64,SUQzAwAA...",
  97.             ogg: "data:audio/ogg;base64,T2dnUwACAAAAAAAAAADLf+..."
  98.         }
  99.  
  100.         this.createAudio();
  101.  
  102.         document.body.addEventListener("click", player.clickHandler = function(){
  103.             player.play();
  104.             document.body.removeEventListener("click", player.clickHandler);
  105.         });
  106.     }
  107.  
  108.     DefaultPlayer.prototype = {
  109.         playQuestion: function(question, prefix) {
  110.             var source_loaders = [], mp3Source, oggSource,
  111.                 player = this;
  112.  
  113.             source_loaders.push(function(next){
  114.                 app.storage.get(question[prefix+"_mp3_crc"], "mp3", function(source){
  115.                     mp3Source = source;
  116.                     next();
  117.                 });
  118.             });
  119.  
  120.             source_loaders.push(function(next){
  121.                 app.storage.get(question[prefix+"_ogg_crc"], "ogg", function(source){
  122.                     oggSource = source;
  123.                     next();
  124.                 });
  125.             });
  126.  
  127.             async.parallel(source_loaders, function(){
  128.                 app.player.load({
  129.                     mp3: mp3Source,
  130.                     ogg: oggSource
  131.                 }, function(){
  132.                     player.play();
  133.                 });
  134.             });
  135.         },
  136.         createAudio: function(){
  137.             var sources = [];
  138.  
  139.             sources.push(dom("source", {"type": "audio/mp3", src: this.media.mp3}));
  140.             sources.push(dom("source", {"type": "audio/ogg", src: this.media.ogg}));
  141.  
  142.             this.audio = dom("audio", sources);
  143.         },
  144.         load: function(media, callback){            
  145.             this.media = media;
  146.  
  147.             this.audio.innerHTML = '';
  148.  
  149.             this.audio.appendChild(dom("source", {
  150.                 "type": "audio/ogg",
  151.                 src: this.media.ogg
  152.             }));
  153.  
  154.             this.audio.appendChild(dom("source", {
  155.                 "type": "audio/mp3",
  156.                 src: this.media.mp3
  157.             }));
  158.  
  159.             this.audio.load();
  160.  
  161.             setTimeout(function(){
  162.                 callback && callback();    
  163.             }, 100);
  164.         },
  165.         play: function(){
  166.             this.audio.play();
  167.         },
  168.         pause: function(){
  169.             this.audio && this.audio.pause();
  170.         },
  171.         getAudioType: function(){
  172.             return "all";
  173.         }
  174.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement