Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Playing Audio Using Keystrokes: Is It Possible?

Annabelle5893

Gold Coder
I'm wondering, how would I make an app that plays audio files using the keys on the number pad? Specifically, I want to make an app that triggers "Rotary Pulse 1" by pressing 1 on the numeric keypad, all the way up to "Rotary Pulse 0" on the 0 key. A dial tone will be toggled on by pressing the spacebar, which will begin with a "receiver pickup" noise. The dial tone turns off when digits are dialed via the numeric keypad. If you press the spacebar either when the dial tone is on, or after dialing a number, a "Receiver Hang Up" noise will be triggered. With the rotary dial sounds, the dial turning to the fingerhole is triggered at key down, and the release of the dial returning to the resting position will be triggered by releasing of the number corresponding to the fingerhole (1, 2, 3, 4, 5, 6, 7, 8, 9, 0). This will be accompanied by the internal sound of the dial pulses as heard in a telephone. What is the code I would use to make this audio app using HTML?
 
This may be more than you asked for, but here goes:
JavaScript:
function response(){switch(number){case "0":var operatorPickup=new Audio('callResponses/OperatorAnswer.wav');operatorPickup.addEventListener("ended",function(){number=prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");if(number==null){number="0";}
operatorPutCallThrough();});operatorPickup.play();break;case "611":var pickup611=new Audio('callResponses/611.wav');pickup611.addEventListener("ended",function(){timeoutHowler();});pickup611.play();break;case "711":var pickup711=new Audio('callResponses/tdd-1.mp3');pickup711.play();break;case "811":var pickup611=new Audio('callResponses/811.wav');pickup811.addEventListener("ended",function(){timeoutHowler();});pickup811.play();break;case "911":var pickup911=new Audio('callResponses/911-xxx-fleet.mp3');pickup911.addEventListener("ended",function(){timeoutHowler();});pickup911.play();break;case "18477651008":var pickupMCI=new Audio('callResponses/MCI.wav');pickupMCI.play();break;case "8675309":var pickup8675309=new Audio('callResponses/discoornis-bell-f1.mp3');pickup8675309.addEventListener("ended",function(){timeoutHowler();});pickup8675309.play();break;case "12124561414":busy();break;case "5551212":busy();

JavaScript:
function response(){
    switch(number){
        case "0":
            var operatorPickup = new Audio('callResponses/OperatorAnswer.wav');
            operatorPickup.addEventListener("ended",function(){
                number=prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");
                if(number == null){
                    number="0";
                }
                operatorPutCallThrough();
            });
            operatorPickup.play();
            break;
        case "611":
            var pickup611 = new Audio('callResponses/611.wav');
            pickup611.addEventListener("ended",function(){
                timeoutHowler();
            });
            pickup611.play();
            break;
        case "711":
            var pickup711 = new Audio('callResponses/tdd-1.mp3');
            pickup711.play();
            break;
        case "811":
            var pickup611 = new Audio('callResponses/811.wav');
            pickup811.addEventListener("ended",function(){
                timeoutHowler();
            });
            pickup811.play();
            break;
        case "911":
            var pickup911 = new Audio('callResponses/911-xxx-fleet.mp3');
            pickup911.addEventListener("ended",function(){
                timeoutHowler();
            });
            pickup911.play();
            break;
        case "18477651008":
            var pickupMCI = new Audio('callResponses/MCI.wav');
            pickupMCI.play();
            break;
        case "8675309":
            var pickup8675309 = new Audio('callResponses/discoornis-bell-f1.mp3');
            pickup8675309.addEventListener("ended",function(){
                timeoutHowler();
            });
            pickup8675309.play();
            break;
        case "12124561414":
            busy();
            break;
        case "5551212":
            busy();
    }
}

Here's the expanded version...
 
when you get that error, can you click on the link the error provides, of where the error is coming from?
When I press "Enter" on the link, it actually reads line numbers within Firefox when posting what the lines say, so here's what's on Line 96.
JavaScript:
96 (else{var pickupCallTrace=new Audio('callResponses/trace-xxx-fleet.mp3');}
 
When I press "Enter" on the link, it actually reads line numbers within Firefox when posting what the lines say, so here's what's on Line 96.
JavaScript:
96 (else{var pickupCallTrace=new Audio('callResponses/trace-xxx-fleet.mp3');}
Well there's the culprit lol.
remove the parethesis in front of else
 
Can you just copy over everything inside of the script tags, <script></script>. That should help out tons 🙂
Sure! Here ya go!
JavaScript:
function reload(){location.reload(true);}
var allowDial;allowDial=true;var availableNumbers=["0","611","711","811","911","*67 + 1 (847) 765-1008","1 (847) 765-1008","867-5309","1 (212) 456-1414","555-1212","555-5555","748-0900","650-0050","650-8950","970-3920","1 (714) 733-9969","*57","*69"];function numberSuggestion(){var randomNumber=Math.floor(Math.random()*(availableNumbers.length));var suggestedNumber=availableNumbers[randomNumber];document.getElementById("suggestion").innerHTML="How about dialing <strong id='suggestedTelephoneNumber'>"+suggestedNumber+"</strong>? Don't like this number? Click the button above again!";}
var dialTone;var isOffHook=false;var callIncoming=false;function receiverRaised(){if(allowDial===false){telephoneAnswered();}
else{offHook();}}
setCallTimeout();function setCallTimeout(){var delay=5000;var delayCaller=Math.floor((Math.random()*8000)+1);delay=delay+delayCaller;setTimeout(incomingCall,delay);}
function ringTelephone(){
   //code for this function here
}

function incomingCall(){
    ringTelephone();

    if(isOffHook===false){
        ringRing.play();
        allowDial=false;
        callIncoming=true;
        setInterval(ringTelephone,5500);
    }
}
function dial1(){if(allowDial){numberDial();number=number+"1";var tone1=new Audio('DTMF Dials/DTMF Tone Dial 1.ogg');tone1.play();}}
function dial2(){if(allowDial){numberDial();number=number+"2";var tone2=new Audio('DTMF Tone Dials/DTMF Tone Dial 2.ogg');tone2.play();}}
function dial3(){if(allowDial){numberDial();number=number+"3";var tone3=new Audio('DTMF Tone Dials/DTMF Tone Dial 3.ogg');tone3.play();}}
function dial4(){if(allowDial){numberDial();number=number+"4";var tone4=new Audio('DTMF Tone Dials/DTMF Tone Dial 4.ogg');tone4.play();}}
function dial5(){if(allowDial){numberDial();number=number+"5";var tone5=new Audio('DTMF Tone Dials/DTMF Tone Dial 5.ogg');tone5.play();}}
function dial6(){if(allowDial){numberDial();number=number+"6";var tone6=new Audio('DTMF Tone Dials/DTMF Tone Dial 6.ogg');tone6.play();}}
function dial7(){if(allowDial){numberDial();number=number+"7";var tone7=new Audio('DTMF Tone Dials/DTMF Tone Dial 7.ogg');tone7.play();}}
function dial8(){if(allowDial){numberDial();number=number+"8";var tone8=new Audio('DTMF Tone Dials/DTMF Tone Dial 8.ogg');tone8.play();}}
function dial9(){if(allowDial){numberDial();number=number+"9";var tone9=new Audio('DTMF Tone Dials/DTMF Tone Dial 9.ogg');tone9.play();}}
function dial0(){if(allowDial){numberDial();number=number+"0";var tone0=new Audio('DTMF Tone Dials/DTMF Tone Dial 0.ogg');tone0.play();}}
function dialStar(){if(allowDial){numberDial();number=number+"*";var toneStar=new Audio('DTMF Tone Dials/DTMF Tone Dial Star.ogg');toneStar.play();}}
function dialPound(){if(allowDial){numberDial();number=number+"#";var tonePound=new Audio('DTMF Tone Dials/DTMF Tone Dial Pound.ogg');tonePound.play();}}
document.onkeydown=function(e){var key=e.keyCode?e.keyCode:e.which;if(e.which===48||e.which===96){dial0();}
if(e.which===49||e.which===97){dial1();}
if(e.which===50||e.which===98){dial2();}
if(e.which===51||e.which===99){dial3();}
if(e.which===52||e.which===100){dial4();}
if(e.which===53||e.which===101){dial5();}
if(e.which===54||e.which===102){dial6();}
if(e.which===55||e.which===103){dial7();}
if(e.which===56||e.which===104){dial8();}
if(e.which===57||e.which===105){dial9();}
if(e.which===106){dialStar();}
if(e.which==109){dialPound();}}
var ringingTone=new Audio('Ringing Tone/Princess 702B Bell Ring.ogg');var timesRung=0;function dial(){allowDial=false;function ring(){ringingTone.play();timesRung++;if(timesRung>1){setTimeout(response,700);}}
ring();setTimeout(ring,4000);}
function response(){switch(number){case "0":var operatorPickup=new Audio('callResponses/OperatorAnswer.wav');operatorPickup.addEventListener("ended",function(){number=prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");if(number==null){number="0";}
operatorPutCallThrough();});operatorPickup.play();break;case "611":var pickup611=new Audio('callResponses/611.wav');pickup611.addEventListener("ended",function(){timeoutHowler();});pickup611.play();break;case "711":var pickup711=new Audio('callResponses/tdd-1.mp3');pickup711.play();break;case "811":var pickup611=new Audio('callResponses/811.wav');pickup811.addEventListener("ended",function(){timeoutHowler();});pickup811.play();break;case "911":var pickup911=new Audio('callResponses/911-xxx-fleet.mp3');pickup911.addEventListener("ended",function(){timeoutHowler();});pickup911.play();break;case "18477651008":var pickupMCI=new Audio('callResponses/MCI.wav');pickupMCI.play();break;case "8675309":var pickup8675309=new Audio('callResponses/discoornis-bell-f1.mp3');pickup8675309.addEventListener("ended",function(){timeoutHowler();});pickup8675309.play();break;case "12124561414":busy();break;case "5551212":busy();break;case "5555555":var pickup5555555=new Audio('callResponses/timeout-xxx-fleet.mp3');pickup5555555.addEventListener("ended",function(){timeoutHowler();});pickup5555555.play();break;case "7480900":var pickupSelf=new Audio('callResponses/partyline-xxx-fleet.mp3');pickupSelf.play();break;case "18005820655":busy();break;case "6508950":var pickupAM=new Audio('callResponses/answering-machine-1.mp3');pickupAM.play();break;case "6500050":var pickupModem=new Audio('modem.wav');pickupModem.play();break;case "9703920":var pickupFax=new Audio('fax-1.mp3');pickupFax.play();break;case "17147339969":var pickup714=new Audio('device-conference.mp3');pickup714.play();break;case "*8217147339969":var pickup714=new Audio('device-conference.mp3');pickup714.play();break;case "*6717147339969":var pickup714block=new Audio('callResponses/reject2-xxx-fleet.mp3');pickup714block.addEventListener("ended",function(){timeoutHowler();});pickup714block.play();break;case "*57":var randNum57=Math.random()>=0.5;if(randNum57){var pickupCallTrace=new Audio('callResponses/trace3-xxx-fleet.mp3');}
else{var pickupCallTrace=new Audio('callResponses/trace-xxx-fleet.mp3');}
pickupCallTrace.addEventListener("ended",function(){timeoutHowler();});pickupCallTrace.play();break;case "*69":var pickupStar69=new Audio('callResponses/return-xxx-fleet.mp3');pickupStar69.addEventListener("ended",function(){timeoutHowler();});pickupStar69.play();break;case "police":var pickupPolice=new Audio('callResponses/policePickup.wav');pickupPolice.addEventListener("ended",function(){var policeComplaint=prompt("Police Department, Sergeant Duffy Speaking.");confirm("Really? You say that "+policeComplaint);var policeHangup=new Audio('callResponses/policeHangup.wav');policeHangup.addEventListener("ended",function(){timeoutHowler();});policeHangup.play();});pickupPolice.play();break;default:var pickupDefault;if(number.length>7){pickupDefault=new Audio('callResponses/ldcircuits-bell-f1.mp3');}
else{pickupDefault=new Audio('callResponses/completeordc-xxx-fleet.mp3');}
pickupDefault.addEventListener("ended",function(){timeoutHowler();});pickupDefault.play();}}
function busy
 
Sure! Here ya go!
JavaScript:
function reload(){location.reload(true);}
var allowDial;allowDial=true;var availableNumbers=["0","611","711","811","911","*67 + 1 (847) 765-1008","1 (847) 765-1008","867-5309","1 (212) 456-1414","555-1212","555-5555","748-0900","650-0050","650-8950","970-3920","1 (714) 733-9969","*57","*69"];function numberSuggestion(){var randomNumber=Math.floor(Math.random()*(availableNumbers.length));var suggestedNumber=availableNumbers[randomNumber];document.getElementById("suggestion").innerHTML="How about dialing <strong id='suggestedTelephoneNumber'>"+suggestedNumber+"</strong>? Don't like this number? Click the button above again!";}
var dialTone;var isOffHook=false;var callIncoming=false;function receiverRaised(){if(allowDial===false){telephoneAnswered();}
else{offHook();}}
setCallTimeout();function setCallTimeout(){var delay=5000;var delayCaller=Math.floor((Math.random()*8000)+1);delay=delay+delayCaller;setTimeout(incomingCall,delay);}
function ringTelephone(){
   //code for this function here
}

function incomingCall(){
    ringTelephone();

    if(isOffHook===false){
        ringRing.play();
        allowDial=false;
        callIncoming=true;
        setInterval(ringTelephone,5500);
    }
}
function dial1(){if(allowDial){numberDial();number=number+"1";var tone1=new Audio('DTMF Dials/DTMF Tone Dial 1.ogg');tone1.play();}}
function dial2(){if(allowDial){numberDial();number=number+"2";var tone2=new Audio('DTMF Tone Dials/DTMF Tone Dial 2.ogg');tone2.play();}}
function dial3(){if(allowDial){numberDial();number=number+"3";var tone3=new Audio('DTMF Tone Dials/DTMF Tone Dial 3.ogg');tone3.play();}}
function dial4(){if(allowDial){numberDial();number=number+"4";var tone4=new Audio('DTMF Tone Dials/DTMF Tone Dial 4.ogg');tone4.play();}}
function dial5(){if(allowDial){numberDial();number=number+"5";var tone5=new Audio('DTMF Tone Dials/DTMF Tone Dial 5.ogg');tone5.play();}}
function dial6(){if(allowDial){numberDial();number=number+"6";var tone6=new Audio('DTMF Tone Dials/DTMF Tone Dial 6.ogg');tone6.play();}}
function dial7(){if(allowDial){numberDial();number=number+"7";var tone7=new Audio('DTMF Tone Dials/DTMF Tone Dial 7.ogg');tone7.play();}}
function dial8(){if(allowDial){numberDial();number=number+"8";var tone8=new Audio('DTMF Tone Dials/DTMF Tone Dial 8.ogg');tone8.play();}}
function dial9(){if(allowDial){numberDial();number=number+"9";var tone9=new Audio('DTMF Tone Dials/DTMF Tone Dial 9.ogg');tone9.play();}}
function dial0(){if(allowDial){numberDial();number=number+"0";var tone0=new Audio('DTMF Tone Dials/DTMF Tone Dial 0.ogg');tone0.play();}}
function dialStar(){if(allowDial){numberDial();number=number+"*";var toneStar=new Audio('DTMF Tone Dials/DTMF Tone Dial Star.ogg');toneStar.play();}}
function dialPound(){if(allowDial){numberDial();number=number+"#";var tonePound=new Audio('DTMF Tone Dials/DTMF Tone Dial Pound.ogg');tonePound.play();}}
document.onkeydown=function(e){var key=e.keyCode?e.keyCode:e.which;if(e.which===48||e.which===96){dial0();}
if(e.which===49||e.which===97){dial1();}
if(e.which===50||e.which===98){dial2();}
if(e.which===51||e.which===99){dial3();}
if(e.which===52||e.which===100){dial4();}
if(e.which===53||e.which===101){dial5();}
if(e.which===54||e.which===102){dial6();}
if(e.which===55||e.which===103){dial7();}
if(e.which===56||e.which===104){dial8();}
if(e.which===57||e.which===105){dial9();}
if(e.which===106){dialStar();}
if(e.which==109){dialPound();}}
var ringingTone=new Audio('Ringing Tone/Princess 702B Bell Ring.ogg');var timesRung=0;function dial(){allowDial=false;function ring(){ringingTone.play();timesRung++;if(timesRung>1){setTimeout(response,700);}}
ring();setTimeout(ring,4000);}
function response(){switch(number){case "0":var operatorPickup=new Audio('callResponses/OperatorAnswer.wav');operatorPickup.addEventListener("ended",function(){number=prompt("Operator, your number please? (Numbers only; enter 'police' for police and emergency)");if(number==null){number="0";}
operatorPutCallThrough();});operatorPickup.play();break;case "611":var pickup611=new Audio('callResponses/611.wav');pickup611.addEventListener("ended",function(){timeoutHowler();});pickup611.play();break;case "711":var pickup711=new Audio('callResponses/tdd-1.mp3');pickup711.play();break;case "811":var pickup611=new Audio('callResponses/811.wav');pickup811.addEventListener("ended",function(){timeoutHowler();});pickup811.play();break;case "911":var pickup911=new Audio('callResponses/911-xxx-fleet.mp3');pickup911.addEventListener("ended",function(){timeoutHowler();});pickup911.play();break;case "18477651008":var pickupMCI=new Audio('callResponses/MCI.wav');pickupMCI.play();break;case "8675309":var pickup8675309=new Audio('callResponses/discoornis-bell-f1.mp3');pickup8675309.addEventListener("ended",function(){timeoutHowler();});pickup8675309.play();break;case "12124561414":busy();break;case "5551212":busy();break;case "5555555":var pickup5555555=new Audio('callResponses/timeout-xxx-fleet.mp3');pickup5555555.addEventListener("ended",function(){timeoutHowler();});pickup5555555.play();break;case "7480900":var pickupSelf=new Audio('callResponses/partyline-xxx-fleet.mp3');pickupSelf.play();break;case "18005820655":busy();break;case "6508950":var pickupAM=new Audio('callResponses/answering-machine-1.mp3');pickupAM.play();break;case "6500050":var pickupModem=new Audio('modem.wav');pickupModem.play();break;case "9703920":var pickupFax=new Audio('fax-1.mp3');pickupFax.play();break;case "17147339969":var pickup714=new Audio('device-conference.mp3');pickup714.play();break;case "*8217147339969":var pickup714=new Audio('device-conference.mp3');pickup714.play();break;case "*6717147339969":var pickup714block=new Audio('callResponses/reject2-xxx-fleet.mp3');pickup714block.addEventListener("ended",function(){timeoutHowler();});pickup714block.play();break;case "*57":var randNum57=Math.random()>=0.5;if(randNum57){var pickupCallTrace=new Audio('callResponses/trace3-xxx-fleet.mp3');}
else{var pickupCallTrace=new Audio('callResponses/trace-xxx-fleet.mp3');}
pickupCallTrace.addEventListener("ended",function(){timeoutHowler();});pickupCallTrace.play();break;case "*69":var pickupStar69=new Audio('callResponses/return-xxx-fleet.mp3');pickupStar69.addEventListener("ended",function(){timeoutHowler();});pickupStar69.play();break;case "police":var pickupPolice=new Audio('callResponses/policePickup.wav');pickupPolice.addEventListener("ended",function(){var policeComplaint=prompt("Police Department, Sergeant Duffy Speaking.");confirm("Really? You say that "+policeComplaint);var policeHangup=new Audio('callResponses/policeHangup.wav');policeHangup.addEventListener("ended",function(){timeoutHowler();});policeHangup.play();});pickupPolice.play();break;default:var pickupDefault;if(number.length>7){pickupDefault=new Audio('callResponses/ldcircuits-bell-f1.mp3');}
else{pickupDefault=new Audio('callResponses/completeordc-xxx-fleet.mp3');}
pickupDefault.addEventListener("ended",function(){timeoutHowler();});pickupDefault.play();}}
function busy
Found one issue... not sure if this may solve it, but:
JavaScript:
var dialTone;var isOffHook=false;var callIncoming=false;function receiverRaised(){if(allowDial===false){telephoneAnswered();}
else{offHook();}}

Can you recognize the issue by just looking at this? 🙂 HINT: this is why it's important to use the expanded version of code when developing/debugging/troubleshooting. Makes things easier to spot, and less headache 🙂
 
Found one issue... not sure if this may solve it, but:
JavaScript:
var dialTone;var isOffHook=false;var callIncoming=false;function receiverRaised(){if(allowDial===false){telephoneAnswered();}
else{offHook();}}

Can you recognize the issue by just looking at this? 🙂 HINT: this is why it's important to use the expanded version of code when developing/debugging/troubleshooting. Makes things easier to spot, and less headache 🙂
That's an image. I can't read that, remember? Or are you wanting me to read the code you copied over?
 
Found one issue... not sure if this may solve it, but:
JavaScript:
var dialTone;var isOffHook=false;var callIncoming=false;function receiverRaised(){if(allowDial===false){telephoneAnswered();}
else{offHook();}}

Can you recognize the issue by just looking at this? 🙂 HINT: this is why it's important to use the expanded version of code when developing/debugging/troubleshooting. Makes things easier to spot, and less headache 🙂
I wonder if the part you're talking about is the line where there's a double version of a closed brace.
 
That's an image. I can't read that, remember? Or are you wanting me to read the code you copied over?
That is not an image 🙂 that is a copy and paste of your code, but to answer your question, the issue is not a "double" close brace... look at the structure of the if statement, when minified, and when not minified.

When not minified
JavaScript:
if(logic){
    //do this if true
}
else {
    //otherwise do this
}

OR
JavaScript:
if(logic){
    //do this if true
}else {
    //otherwise do this
}

Now let's take a look at the structure, when minified:
JavaScript:
if(logic){//do this if true} else {//otherwise do this}
 
Last edited:
That is not an image 🙂 that is a copy and paste of your code, but to answer your question, the issue is not a "double" close brace... look at the structure of the if statement, when minified, and when not minified.

When not minified
JavaScript:
if(logic){
    //do this if true
}
else {
    //otherwise do this
}

OR
JavaScript:
if(logic){
    //do this if true
}else {
    //otherwise do this
}

Now let's take a look at the structure, when minified:
JavaScript:
if(logic){//do this if true} else {//otherwise do this}

Hence why it makes it so much easier to debug when the code is not a "one liner" as it is when minified
 
I'm just confused about the "Missing ) before formal parameters" error on Line 101, when all that line says is </script>
Can you unminify all of your code, that is, can you expand all of your code, and paste it all here? I think last time there was a cut-off and not all the code was posted. I had to go in and add a few closing braces towards the end just to make it valid js code. Once you expand all of it, the issue should be easier to identify.
 
Back
Top Bottom