Javascript 如何在网页画布上运行的Processing.js程序中播放声音?
我在Processing.js中构建了一个相当大的游戏(10000行代码),通过加载库可以在网页中运行它。以下是一个例子:Javascript 如何在网页画布上运行的Processing.js程序中播放声音?,javascript,html,audio,canvas,processing.js,Javascript,Html,Audio,Canvas,Processing.js,我在Processing.js中构建了一个相当大的游戏(10000行代码),通过加载库可以在网页中运行它。以下是一个例子: <!DOCTYPE html> <html> <head> <title>Processing.JS in Webpage</title> </head> <body> <canvas id="mycanvas"></ca
<!DOCTYPE html>
<html>
<head>
<title>Processing.JS in Webpage</title>
</head>
<body>
<canvas id="mycanvas"></canvas>
</body>
<script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
<script>
var programCode = function(processingInstance) {
with (processingInstance) {
size(400, 400);
background(100,200,0)
frameRate(60);
noStroke()
fill(255, 255, 0);
ellipse(200, 200, 200, 200);
noFill();
stroke(0, 0, 0);
strokeWeight(5);
arc(200, 205, 150, 132, 0, PI);
fill(0, 0, 0);
ellipse(250, 180, 10, 10);
ellipse(153, 180, 10, 10);
mousePressed = function(){
background(255,0,0);
//how to play a sound file here?
};
}};
// Get the canvas that ProcessingJS will use
var canvas = document.getElementById("mycanvas");
// Pass the function to ProcessingJS constructor
var processingInstance = new Processing(canvas, programCode);
</script>
</html>
谢谢
Sound = new Audio('https://upload.wikimedia.org/wikipedia/commons/6/6e/%28Audio_Bahasa_Melayu%29_ibu.ogg');
Sound.play();
Processing.JS有一个声音库。您可以使用此代码播放您有URL的任何声音
让我们来看看。请注意,Processing.js自2018年12月我停止使用以来就不再是一件事了。你真的不想再使用它了。但即便如此,您使用的版本还是很古老的,在我删除它之前的最后一个版本是2017年3月的1.6.6。您的1.4.8从2014年4月起生效。所以,“不要使用它,但如果你必须使用它,至少要使用最近发布的版本”。记住Processing和Processing.js不是一回事。@Mike'Pomax'Kamermans你说得很对。我已经更新了我的答案。
Sound = new Audio('https://upload.wikimedia.org/wikipedia/commons/6/6e/%28Audio_Bahasa_Melayu%29_ibu.ogg');
Sound.play();