简体   繁体   English

如果没有用户交互,p5.js 将无法工作

[英]p5.js won't working without user interaction

 let gameFound; function preload() { gameFound = loadSound('/audio/SFX_-_Game_found_03.mp3'); } function setup() { createCanvas(400, 400); } function gameFoundSound(){ gameFound.play(); } gameFoundSound();

Hi all here I am sharing my code I want play sound when my popup is appear there without user interaction but in browser getting warning - The AudioContext was not allowed to start.大家好,我正在分享我的代码,我想在没有用户交互的情况下出现我的弹出窗口时播放声音,但在浏览器中收到警告 - 不允许启动 AudioContext。 It must be resumed (or created) after a user gesture on the page and getting error - Uncaught TypeError: Cannot read property 'play' of undefined它必须在页面上的用户手势并出现错误后恢复(或创建) - Uncaught TypeError: Cannot read property 'play' of undefined

How I can play background music, please help me.我怎样才能播放背景音乐,请帮助我。

Did you call the function preload ?你打电话给function preload了吗?

Without calling it gameFound is undefined不调用它gameFound是未定义的

You must call gameFoundSound() in function setup().您必须在 function setup() 中调用 gameFoundSound()。 Uncaught TypeError: Cannot read property 'play' of undefined happens when the sound (gameFound in this case) is not ready. Uncaught TypeError: Cannot read property 'play' of undefined当声音(在本例中为 gameFound)未准备好时发生。 Calling loadSound() in function preload() ensures that the sound is loaded before function setup() and draw().在 function preload() 中调用 loadSound() 确保声音在 function setup() 和 draw() 之前加载。 An alternative would be using callback of loadSound(), when calling it outside of function preload().当在 function preload() 之外调用它时,另一种方法是使用 loadSound() 的回调。 More info.更多信息。 in the doc: https://p5js.org/reference/#/p5/loadSound .在文档中: https://p5js.org/reference/#/p5/loadSound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM