如果要加入音效,還要多加一個 minim.js ,而在 .pde內則需要 import minim.js; ,但在電腦上則是 import ddf.minim.*;
這裡附上網上找到的 minim.js 但本來只能輸出 ogg 和 mp3 ,我改成輸出 wav 和 mp3 檔的。
/**
* Minim-emulation code by Daniel Hodgin
*/
// wrap the P5 Minim sound library classes
function Minim() {
this.loadFile = function (str) {
return new AudioPlayer(str);
}
}
// Browser Audio API
function AudioPlayer(str) {
var loaded = false;
var looping = false;
if (!!document.createElement('audio').canPlayType) {
var audio = document.createElement('audio');
audio.addEventListener('ended', function () {
if (looping) {
this.currentTime = 0;
this.play();
}
}, false);
audio.preload = 'auto';
audio.autobuffer = true;
if (canPlayWav()) {
audio.src = str.split(".")[0] + ".wav";
} else if (canPlayMp3()) {
audio.src = str;
}
loaded = true;
}
this.play = function () {
if (!loaded) {
var local = this;
setTimeout(function() { local.play(); }, 50);
return;
}
audio.play();
};
this.loop = function () {
if (!loaded) {
var local = this;
setTimeout(function() { local.loop(); }, 50);
return;
}
//audio.loop = 'loop';
looping = true;
audio.play();
};
this.pause = function () {
if (!loaded) {
return;
}
audio.pause();
};
this.rewind = function () {
if (!loaded) {
return;
}
// rewind the sound to start
if(audio.currentTime) {
audio.currentTime = 0;
}
};
this.position = function() {
if (!loaded) {
return -1;
}
if(audio.currentTime) {
return audio.currentTime * 1000;
}
return -1;
};
this.cue = function(position) {
if (!loaded) {
return;
}
if(audio.currentTime) {
audio.currentTime = position / 1000;
}
};
this.mute = function() {
audio.volume = 0.0;
};
this.unmute = function() {
audio.volume = 1.0;
};
}
function canPlayWav() {
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/wav; codecs="vorbis"').replace(/no/, ''));
}
function canPlayMp3() {
var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mp3;').replace(/no/, ''));
} |