Call Us Today! 877.742.2583




Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

0. About


Excerpt

Example of calling a dtmf callback function while playing a file.

...

Example JS Code

...


1. Example JS Code 

Note
titleA, B, C, D tones

Make sure to expect (and handle) the DTMF tones A , B , C , and D; these are very rare nowadays but they are still possible to send (e.g., someone has an old device with the extra ABCD column). See DTMF for more.


Code Block
languagejs
titleDTMF callback example
/* example of calling a dtmf callback function while playing a file. */
function onPlayFile(s, type, obj, arg)
{
   try {
      if (type == "dtmf") {
         console_log("info", "DTMF digit: " + s.name + " [" + obj.digit + "] len [" + obj.duration + "]\n\n");

         // below are some controls available to javascript input callback
         // not all are 100% tested in this example, please check mod_spidermonkey if they do not work for you
         // if you do not require playback control, the entire following block can be removed
         if (obj.digit == "3") {
            return "seek:+5000"; // seek forwards (testing needed)
         }
         else if (obj.digit == "1") {
            return "seek:-5000"; // seek backwards (testing needed)
         }
         else if (obj.digit == "2") {
            return "volume:+1"; // volume up (testing needed)
         }
         else if (obj.digit == "3") {
            return "volume:-1"; // volume down (testing needed)
         }
         else if (obj.digit == "5") {
            return "pause"; // pauses and unpauses
         }
         else if (obj.digit == "6") {
            return "restart"; // seek to the beginning of the file
         }
         else if (obj.digit == "7") {
            return "speed:+1"; // increase playback speed (testing needed)
         }
         else if (obj.digit == "8") {
            return "speed:-1"; // decrease playback speed (testing needed)
         }
         else if (obj.digit == "9") {
            return true; // return without interrupting playback of streamFile
         }
         else if (obj.digit == "*") {
            return false; // stop playback and break from streamFile
         }

      }
   }  catch (e) {
      console_log("err", e + "\n");
   }
   return true;
}

session.answer();

while(session.ready()) {
   session.streamFile(argv[0], onPlayFile);
}

...