Javascript:为什么音频没有播放?

时间:2015-11-15 23:25:19

标签: javascript audio

我之前提出过有关将mp3文件添加到我的javascript代码中的问题的建议,并提出了这个问题。

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.");
}

我不确定我的语法是否有问题,或者我是不是让我的音频文件对程序可见。也有人建议网站运行我的代码,因为我使用的那个可能不播放音频。只是一个理论。

2 个答案:

答案 0 :(得分:0)

您希望了解ifelse ifelse语句的工作原理。

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

要探索其他错误,请在浏览器中打开开发人员工具(最好使用Chrome)并查看控制台中是否有任何错误(例如获取音频文件等问题...)

Using local file for Web Audio API in Javascript

答案 1 :(得分:0)

我会推荐您回答问题here

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

这是播放音频的基础。从Roko C. Buljan给出的链接播放音频后,请稍后查看条件