ReferenceError: soundFormats is not defined

ReferenceError: soundFormats is not defined

我正在尝试了解 p5 并且一直在学习教程 (https://p5js.org/reference/#/p5.SoundFile)。

function preload() {
  soundFormats('mp3', 'ogg');
  mySound = loadSound('assets/cam.mp3');
}

function setup() {
  mySound.setVolume(0.1);
  mySound.play();
}

除了切换到我自己的测试歌曲外,我已经逐字按照文档进行了。当我在我的 repl.it https://repl.it/@JacksonEnnis/Coloquial 上 运行 时,我收到一条错误消息 "ReferenceError: soundFormats is not defined"。但是,我知道此功能已定义,因为它来自文档。我用谷歌搜索了这个问题,但这似乎不是一个常见的问题。

如果有人明白为什么会这样,请向我解释一下,以便我学习。

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <script language="javascript" type="text/javascript" src="path/to/p5.sound.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
    <script src="script.js"></script>
  </body>
</html>

只是总结评论。 这是有效的解决方案:

<!doctype HTML>
<html>
<head>
  <html><head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.sound.js"></script>

</head>
<body>
  <script> 
    function preload() {
      soundFormats('ogg', 'mp3');
      mySound = loadSound('https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3');
    }

    function setup() {
      mySound.setVolume(1);
      // mySound.play();
      mySound.loop();
    }
  </script>
</body>
</html>

如您所见:还必须包含 p5.sound.js 文件。它必须是有效路径,并且必须在 p5.js.

之后加载

soundFormats 是 p5.sound.js 中定义的函数。如果此 javascript 文件未正确加载,则会出现错误消息 "soundFormats is not defined"。