【问题标题】:can't open sound file although the path is right tried ./ , ../ , ../../ and none worked尽管路径正确但无法打开声音文件 ./ , ../ , ../../ 都没有
【发布时间】:2020-10-02 19:49:21
【问题描述】:

尝试制作二十一点游戏,当我按下点击按钮时出现我的错误,应该出现一张王牌图片,并且应该播放声音,但没有播放声音并且出现此错误 无法加载资源:net ::ERR_FILE_NOT_FOUND

这是我到项目文件夹的路径

我试图改变输入源代码的方式,例如 /statics , ../../statics 但它不起作用

这是我需要理解的代码部分:

html部分

    <div class="playerContainer">
        <div class="playerChoice">
            <h1 id="playerScore"> You: <span id="playerResult"> 0 </span> </h1>
        </div>
        <div class="botChoice">
            <h1 class="dealerResult"> Computer: <span id="dealerResult"> 0 </span> </h1>
        </div>
    </div>
    <div class="controls">
        <button id="standBtn"class="btn-lg btn-warning mr-2">stand</button>
        <button id="dealBtn"class="btn-lg btn-danger mr-2">deal</button>
        <button id="hitBtn"class="btn-lg btn-primary mr-2">hit</button>
    </div>

js部分

let blackJackGame = {
    'you' : {'scoreSpan':'#playerResult','div': '.playerChoice', 'score': 0},
    'bot' : {'scoreSpan':'#dealerResult','div': '.botChoice', 'score': 0}
};

const YOU = blackJackGame['you'];
const BOT = blackJackGame['bot'];
const hitSound = new Audio('../statics/sounds/Ta Da-SoundBible.com-1884170640');

document.querySelector('#hitBtn').addEventListener('click',blackJackHit);

function blackJackHit() {
    let cardImage = document.createElement('img');
    cardImage.src = 'B:/Html, CSS & Js/js excersing/photos/KS.png'
    document.querySelector(YOU['div']).appendChild(cardImage);
    hitSound.play();
}

这个js行出现错误

const hitSound = new Audio('../statics/sounds/Ta Da-SoundBible.com-1884170640');

【问题讨论】:

  • 如果它来自你的index.html,我想你只需要/sounds/Ta Da-SoundBible.com-1884170640
  • 刚刚试了,出现同样的错误

标签: javascript path


【解决方案1】:
  1. 您需要在路径末尾添加音频文件扩展名。
  2. 看来正确的路径应该是../sounds/file.extension

由于您使用的是 VSCode,我建议您使用 Path Intellisense 扩展名,以便将来更轻松地处理类似情况。

【讨论】:

  • 如果您的意思是在文件名后添加 .extension,我已经这样做了,并且出现了同样的错误。
  • 除了使用文件扩展名之外,您是否尝试过使用我在第 2 点中建议的路径?如果上述建议失败,您是否尝试过 Path Intellisense?
  • 是的,我已经尝试完全按照你说的做,但不幸的是它没有用,但我没有尝试扩展,因为我需要重新安装 vbcode 才能让它们工作
  • 我刚刚尝试了扩展,它可以找到文件,但运行时遇到同样的问题
  • 您确定上述错误与音频文件有关,而不是与具有此绝对路径的图像有关吗? B:/Html, CSS &amp; Js/js excersing/photos/KS.png 请尝试用相对路径替换它并确保错误与它无关并检查它是否出现,如果不是这种情况,请确保文件没有损坏并尝试播放并让我知道。
猜你喜欢
  • 1970-01-01
  • 2015-11-27
  • 2015-07-27
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多