【问题标题】:Javascript: Why is the audio not playing?Javascript:为什么没有播放音频?
【发布时间】:2015-11-16 08:24:35
【问题描述】:

我从之前询问的关于将 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.");
}

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

【问题讨论】:

  • 你还有很长的路要走,因为我假设你不太了解 HTML、JS 或 CSS。抱歉,如果我错了,但我正在尝试建立和评估您的知识。你的问题需要一个很大比例的答案。您是否有使用其他语言(如 C、C++ 或 Java)的经验?
  • 不,我不是要他那样做,请看我的意思。

标签: javascript audio


【解决方案1】:

我建议您参考已回答的问题here

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

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

【讨论】:

    【解决方案2】:

    您想了解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

    【讨论】:

    • 感谢您澄清这一点,因为我之前已经编写了带有所有单独的“else-if”条件的代码,但我在另一个网站上看到这样添加加号以使程序更短。这就解释了为什么在我输入音频之前它就表现得很奇怪。
    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 2018-03-12
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多