This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

sound
#5
Hi,

This is a very old script and was used before we had custom JS.

Due the implemantation of custom JS we had better options to implement things..

Use this custom JS instead of the old script above:
Code:
// Closing current script section to add elements
</script>
<audio id="SE_Audio" style="display: none;"></audio>
<script type="text/javascript"> // Starting new script section after adding elements 

$(function(){

   // Table with parameters address, audio URL, repeat, interval in ms
   var AudioTable = [
     { address:"32/0/0", url:"/user/beep.mp3", repeat:true, interval: 2000, intervalHandle: 0},
     { address:"32/0/1", url:"/user/beep1.mp3", repeat:true, interval: 2000, intervalHandle: 0},
     { address:"32/0/2", url:"/user/beep.mp3", repeat:false, interval: 2000, intervalHandle: 0},
     { address:"32/0/3", url:"/user/beep1.mp3", repeat:false, interval: 2000, intervalHandle: 0},
   ];
        
   var ctx = new(window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext), request = new XMLHttpRequest();
   var snd = document.getElementById("SE_Audio");
 
  function getMobileOperatingSystem() {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;
    //console.log(userAgent)
    if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
      return 'iOS';
    } else if ( userAgent.match(/Android/i)) {
      return 'Android';
    } else if ( userAgent.match(/iKonWebTouch/i)) {
      return 'U.Motion';
    } else {
      return 'PC';
    }
  }

  var OS_Version = getMobileOperatingSystem();
 
  // Fix iOS Audio Context
  (function() {
    var fixAudioContext = function (e) {
      if (ctx) {
        // Create empty buffer
        var buffer = ctx.createBuffer(1, 1, 22050);
        var source = ctx.createBufferSource();
        source.buffer = buffer;
        // Connect to output
        source.connect(ctx.destination);
        // Play sound
        if (source.start) {
          source.start(0);
        } else if (source.play) {
          source.play(0);
        } else if (source.noteOn) {
          source.noteOn(0);
        }
      }
      // Check if document is loaded in iframe
      if (window.frameElement){
        // Remove event listeners from parent
        var thisparent = window.parent;
        thisparent.document.removeEventListener('touchstart', fixAudioContext);
        thisparent.document.removeEventListener('touchend', fixAudioContext);
      }
      // Remove events
      document.removeEventListener('touchstart', fixAudioContext);
      document.removeEventListener('touchend', fixAudioContext);
    };
    // Check if document is loaded in iframe
    if (window.frameElement){
      // Add event listeners to parent
      var thisparent = window.parent;
      // Event listener for iOS 6-8 (was previous touchstart event)
      thisparent.document.addEventListener('touchstart', fixAudioContext);
      // Event listener for iOS 9+ (is now touchend event)
      thisparent.document.addEventListener('touchend', fixAudioContext);
    }
    // Event listener for iOS 6-8 (was previous touchstart event)
    document.addEventListener('touchstart', fixAudioContext);
    // Event listener for iOS 9+ (is now touchend event)
    document.addEventListener('touchend', fixAudioContext);
  })();
 
  // Function to play audio by URL
  function Play_Audio(url){
    //console.log(OS_Version)
    if (OS_Version === 'Android' || OS_Version === 'U.Motion' ){
        //alert(OS_Version);
      snd.src = url;
        snd.pause();
        sndcurrentTime = 0;
      //snd.load();
      snd.play();
    } else {
      request.open("GET", url, true);
      request.responseType = "arraybuffer";
      request.onload = function(){
          ctx.decodeAudioData(request.response, onDecoded);
      }
      function onDecoded(buffer){
          var bufferSource = ctx.createBufferSource();
          bufferSource.buffer = buffer;
          bufferSource.connect(ctx.destination);
          bufferSource.start();
      }
      request.send();
    }
  };
 
  // Function to create event listeners
  function CreateListeners(groupaddr, url, repeat, interval, item){
    // Make event listener to object
    if (typeof grp != 'undefined') {
      grp.listen(groupaddr, function(object, state) {
        var value = object.value;
        if (state == 'value') {
          if (value == 1) {
            if (repeat == true){
              if (AudioTable[item].intervalHandle === null) {
                Play_Audio(url);
                  AudioTable[item].intervalHandle = setInterval(function() {
                                    Play_Audio(url);
                                }, interval);
              }
              } else {
                Play_Audio(url);
              }
          } else {
            if (AudioTable[item].intervalHandle !== null) {
                   clearInterval(AudioTable[item].intervalHandle);
                   AudioTable[item].intervalHandle = null;
               }
        }
        }
      }, true);
    }
    }
 
  // Start creating listeners
  for (var i in AudioTable) {
    CreateListeners(AudioTable[i].address, AudioTable[i].url, AudioTable[i].repeat, AudioTable[i].interval, i);
  }
 
});
BR,

Erwin
Reply


Messages In This Thread
sound - by balatis - 25.03.2020, 12:37
RE: sound - by admin - 25.03.2020, 13:15
RE: sound - by balatis - 26.03.2020, 20:56
RE: sound - by Erwin van der Zwart - 23.08.2020, 09:41
RE: sound - by Domoticatorino - 21.08.2020, 14:25
RE: sound - by Domoticatorino - 23.08.2020, 15:46

Forum Jump: