jc92me
jc92me

Reputation: 27

Javascript: Why is the audio not playing?

I have taken advice from previous questions I asked about adding mp3 files to my javascript code and came up with this.

var howrutday = new Audio('file:///C:/This_PC/Music/Science_Fair_Robot/howrutday.mp3');


var name = function (robot) {
    if ('Hi.' + 'Hi!' + 'Hello.' + 'Hello!' + 'Greetings.' + 'Greetings!') {
    console.log("How are you doing today " + name + "?");
    howrutday.play();
}   else if ('Good morning.' + 'Good morning!') {
    console.log("How are you doing today " + name + "?");
}   else if ('Good afternoon.' + 'Good afternoon!') {
    console.log("And to you also, what a lovely day.");
}   else if ('Good evening.' + 'Good evening!') {
    console.log("How was your day?");
}   else if ('Good night.' + 'Good night!') {
    console.log("Rest well " + name + ". I hope to see you tomorrow.");
}   else {
    console.log("Try saying that again but make sure you are using proper grammar like applying punctuation and capitalization.");
}

I'm not sure if it's a problem with my syntax or if I'm not making my audio file visible to the program. Also does anybody have recommendations for websites to run my code in because maybe the one I'm using does not play audio. Just a theory though.

Upvotes: 1

Views: 18037

Answers (2)

Liem Ly Quan
Liem Ly Quan

Reputation: 71

I would refer you to the question that is answered here.

var audio = new Audio('audio_file.mp3');
audio.play();

That 's the basic of playing audio. Have a look at conditional later after the audio is playing from the link that Roko C. Buljan has given

Upvotes: 3

Roko C. Buljan
Roko C. Buljan

Reputation: 206102

you would like review how if, else if and else statements work.

if ('Hi.' + 'Hi!' + 'Hello.' + 'Hello!' + 'Greetings.' + 'Greetings!') {
    // The above statement will always evaluate to boolean `true`
    // Everything in here will always run and no `else`, `else if` will ever trigger

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

To explore for additional errors, open Developer Tools in your browser (Preferably use Chrome) and see if any errors appear in Console ( like issues in getting the audio file etc...)

Using local file for Web Audio API in Javascript

Upvotes: 1

Related Questions