【问题标题】:First Time SoundJS User, Can't Seem To Get A Sound To Play首次使用 SoundJS 用户,似乎无法播放声音
【发布时间】:2018-12-05 22:20:39
【问题描述】:

我只是...试图让 SoundJS 播放声音。看起来文件永远不会加载,因为我的 handleLoad 函数永远不会被调用。有什么想法吗?

    var audioPath = "snd/";
    var sounds = [
        { id: "mySound", src: "gaia.mp3" }
    ];

    function init( ) {
        createjs.Sound.addEventListener( "fileload", handleLoad );
        createjs.Sound.registerSounds( sounds, audioPath );
    }

    function handleLoad( event ) {
        console.log( "Hello from handleLoad" );
    }

init 运行时出现“creates.Sound.registerSounds 不是函数”错误。

【问题讨论】:

  • 如果您粘贴的错误消息与显示的完全一致,那么问题是您的程序有错字。错误消息中显示的函数名称有creates,而它应该有createjs。但是示例代码中的名称是正确的。您是粘贴代码示例,还是重新输入?
  • 我重新输入了它,我错字错过了“j”。 :( 真的很好的想法。这是从控制台复制的完整错误:TypeError: createjs.Sound.registerSounds is not a function. (In 'createjs.Sound.registerSounds(sounds, audioPath)', 'createjs.Sound.registerSounds ' 未定义)
  • 啊,在这种情况下,它必须是另一个错字:-) 根据createjs.com/getting-started/soundjs,您想要的函数名称是createjs.Sound.registerSound,而不是registerSounds,最后是s
  • @ottomeister - 有 registerSoundregisterSounds API。一个接受单个项目,另一个接受一个数组。 createjs.com/docs/soundjs/classes/…

标签: javascript createjs soundjs


【解决方案1】:

您的演示应该可以工作。

我将您的代码复制到 JSFiddle 中,并将其指向我知道存在的声音。https://jsfiddle.net/lannymcnie/vj8rLghb/

唯一的区别是我添加了第三个参数,因为它是跨域加载的

createjs.Sound.addEventListener("fileload", handleLoad, true);

此外,Chrome 中的更改意味着音频无法在没有用户交互的情况下播放,因此一旦加载了音频,我添加了一个文档点击侦听器,以便在您在任意位置单击页面时播放。

你能发布你在控制台中得到的完整错误吗?不清楚registerSounds 是否未定义,或者undefined 上不存在,这表明createjs.Sound 未定义。您是否确保正确包含 SoundJS?尝试将其放入控制台:

console.log(createjs.Sound);

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多