Advertisement
d34n

Untitled

Jun 3rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. My workaround in an app that allows choosing of system sounds that may or may not have ANDROID_LOOP=true set involves checking the file and making a note as follows:
  2.  
  3. window.resolveLocalFileSystemURL($('#soundfile').val(), function(entry){
  4. console.log('got file entry=',entry);
  5. entry.file(function(file){
  6. var reader = new FileReader();
  7. reader.onload = function(e) {
  8. this.result = this.result.substr(0,1024);
  9. if(this.result.toLowerCase().indexOf("android_loop=true")>0){
  10. console.log("ANDROID_LOOP=true detected!");
  11. settings.alarm_looper=true;
  12. } else if(settings.hasOwnProperty('alarm_looper')) delete settings.alarm_looper;
  13. // async, so..
  14. play_sound($('#soundfile').val());
  15. localStorage.setItem('settings',JSON.stringify(settings));
  16. }
  17. reader.readAsText(file);
  18. });
  19. }, function(e){
  20. console.log('failed to get file e=',e);
  21. if(settings.hasOwnProperty('alarm_looper')) delete settings.alarm_looper;
  22. });
  23.  
  24. Then I use a setInterval that detects duration to play the sound:
  25.  
  26. sound_start=new Date().getTime();
  27. media1.play({ numberOfLoops: 1 });
  28. // hack for bug where files with ANDROID_LOOP=true always loop
  29. if(settings.alarm_looper){
  30. sound_end=null;
  31. if(!handle.hasOwnProperty('media')) handle.media=setInterval(function(){
  32. // wait until media gives us the duration..
  33. if(media1._duration==-1){
  34. console.log('media1._duration=-1, interval return');
  35. return;
  36. } else if(sound_end===null) sound_end=Math.round(sound_start+(media1._duration*1000)-40);
  37.  
  38. var now=new Date().getTime();
  39. if(now>=sound_end){
  40. console.log('now>=sound_end '+now+'>='+sound_end+' killing media');
  41. if(media1!=null) media1.release();
  42. if(handle.hasOwnProperty('media')){
  43. clearInterval(handle.media);
  44. delete handle.media;
  45. }
  46. sound_end=null;
  47. }
  48. }, 50);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement