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?
 
I wonder if this would look better.
JavaScript:
function dial1() {
    if(allowDial == true) {
        numberDial();
        number=number+"1";
        var DTMFToneDial1=new Audio('file:///M:/Documents/Virtual%20Phone/DTMF%20Tone%20Dials/DTMF%20Tone%20Dial%201.ogg');
        DTMFToneDial1.play();
    case 49,97:
        dial1();
}
 
I wonder if this would look better.
JavaScript:
function dial1() {
    if(allowDial == true) {
        numberDial();
        number=number+"1";
        var DTMFToneDial1=new Audio('file:///M:/Documents/Virtual%20Phone/DTMF%20Tone%20Dials/DTMF%20Tone%20Dial%201.ogg');
        DTMFToneDial1.play();
    case 49,97:
        dial1();
}
That will crash and error out due to that case statement you have there....Also you would be missing a close } for the if statement
 
That will crash and error out due to that case statement you have there....Also you would be missing a close } for the if statement
How about this?
JavaScript:
function dial1() {
    if(allowDial == true) {
        numberDial();
        number=number+"1";
}
        var DTMFToneDial1=new Audio('file:///M:/Documents/Virtual%20Phone/DTMF%20Tone%20Dials/DTMF%20Tone%20Dial%201.ogg');
        DTMFToneDial1.play();
    if(event.keycode ==49 | event.keycode==97);
        dial1();
}
 
How about this?
JavaScript:
function dial1() {
    if(allowDial == true) {
        numberDial();
        number=number+"1";
}
        var DTMFToneDial1=new Audio('file:///M:/Documents/Virtual%20Phone/DTMF%20Tone%20Dials/DTMF%20Tone%20Dial%201.ogg');
        DTMFToneDial1.play();
    if(event.keycode ==49 | event.keycode==97);
        dial1();
}
question: why do you feel like you need the if statement with the event.keycode there?
 
I wish I knew what's best for this app. Should I put the case statements instead of "if" statements for the keycodes?
Code:
<script>
switch (event.keycode){
    case 49,97:
        dial1();
        break;
    ...
    default:
        hasDialedNumber = false;
    case 50,98:
        dial2();
        break;
    ...
    case 51,99:
        dial3();
        break;
    ...
    case 52,100:
        dial4();
        break;
    ...
    case 53,101:
        dial5();
        break;
    ...
    case 54,102:
        dial6();
        break;
    ...
    case 55,103:
        dial7();
        break;
    ...
    case 56,104:
        dial8();
        break;
    ...
    case 57,105:
        dial9();
        break;
    ...
    case 106:
        dialStar();
        break;
    ...
    case 48,96:
        dial0();
        break;
    ...
    case 109:
        dialPound();
        break;
    ...
</script>
 
I wish I knew what's best for this app. Should I put the case statements instead of "if" statements for the keycodes?
Code:
<script>
switch (event.keycode){
    case 49,97:
        dial1();
        break;
    ...
    default:
        hasDialedNumber = false;
    case 50,98:
        dial2();
        break;
    ...
    case 51,99:
        dial3();
        break;
    ...
    case 52,100:
        dial4();
        break;
    ...
    case 53,101:
        dial5();
        break;
    ...
    case 54,102:
        dial6();
        break;
    ...
    case 55,103:
        dial7();
        break;
    ...
    case 56,104:
        dial8();
        break;
    ...
    case 57,105:
        dial9();
        break;
    ...
    case 106:
        dialStar();
        break;
    ...
    case 48,96:
        dial0();
        break;
    ...
    case 109:
        dialPound();
        break;
    ...
</script>
I thought we had talked about removing the ... from the switch case? Also, typically the default statement goes at the very end, after all your case statements. As far as your question is concerned... you can do either or. But let's not add more confusion to the mix, shall we? Let's stick to my question and go from there...
 
I wonder if this would look better.
JavaScript:
function dial1() {
    if(allowDial == true) {
        numberDial();
        number=number+"1";
        var DTMFToneDial1=new Audio('file:///M:/Documents/Virtual%20Phone/DTMF%20Tone%20Dials/DTMF%20Tone%20Dial%201.ogg');
        DTMFToneDial1.play();
    case 49,97:
        dial1();
}
Let's start here again.... When I asked you why you needed the case statement here, you changed it to an if statement... notice that you don't have a switch statement here so that would have caused an error... When you changed it to an if statement, it would have also errored out because there is no variable called "event" in the function... which is why I asked why you felt like that would be needed. You are already looking for the event in the function for the document.onkeydown
 
I think I may have fixed the look by removing all the instances of "dot dot dot" (...), and put a closing brace (}) at the bottom after all the case statements. Does this look right?
JavaScript:
switch (event.keycode) {
    case 49,97:
        dial1();
        break;
    default:
        hasDialedNumber = false;
    case 50,98:
        dial2();
        break;
    case 51,99:
        dial3();
        break;
    case 52,100:
        dial4();
        break;
    case 53,101:
        dial5();
        break;
    case 54,102:
        dial6();
        break;
    case 55,103:
        dial7();
        break;
    case 56,104:
        dial8();
        break;
    case 57,105:
        dial9();
        break;
    case 106:
        dialStar();
        break;
    case 48,96:
        dial0();
        break;
    case 109:
        dialPound();
        break;
}
 
Back
Top Bottom