音频文件无法在 html 中播放

问题描述 投票:0回答:2

所以我创建了一个网站,该网站应该将人们重定向到另一个网站,在那里他们将被骗。 rick 的 GIF 工作正常,但由于某种原因我无法播放音频文件。重定向到新网站工作正常,没有任何问题。

这是 HTML:

<!DOCTYPE html>
<html lang="no">
    <head>
        <title>Nettsidens tittel</title>
        <meta charset="utf-8">
        <link id="cssFil" rel="stylesheet" type="text/css" href="nyhetsbrev2.css">
    </head>
    <body id="wrapper">
        <h2 id="prank">PRANNKK!!</h2>

        <img id="bilde" alt="bilde" src="https://i.pinimg.com/originals/89/5c/e7/895ce751ba0379700381d17a67086931.gif">
     
        
        <audio id="audi" src="./RickRoll.mp3" autoplay></audio>
        
        

        <script>
        
            let audioo = document.getElementById("audi");
            audi.play();
            console.log("audio funksjonen kjører");

        </script>
      


    </body>
</html>

这是CSS

* {
    box-sizing: border-box;
    margin: 0px;
  }

#prank {
    color:red;
    text-align: center;
    margin-top:2em;
}

#wrapper {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
  }

  body {
    background-color: lightblue;
  }

  #bilde {
    justify-content: center;
    width: 30vw;
    margin-left: auto;
    margin-right: auto;
    padding: 4vw;
  }

我尝试在音频标签中使用自动播放,但这不起作用。 mp3文件与html和css文件位于同一文件夹中,因此我认为程序看不到mp3文件。

javascript html css audio
2个回答
0
投票
        <script>
            document.getElementById("playButton").addEventListener("click", function() {
                let audioo = document.getElementById("audi");
                audioo.play();
                console.log("audio funksjonen kjører");
            });
        </script>

您使用的代码中有拼写错误

audi.play();

但是你需要使用

audioo.play();
作为你分配的变量作为
audioo


0
投票

首先你有一个拼写错误“audi.play()”应该是“audioo.play” 你为什么不试试这个呢

<audio id="Audi">
  <source src="./RickRoll.mp3" type="audio/mpeg">
</audio>
<script>
    let audioo = document.getElementById("audi");
    audioo.play();
</script>
© www.soinside.com 2019 - 2024. All rights reserved.