を働く私は、HTML5の響板に取り組んでいますが、私は思わぬ障害のビットをヒットしてきました...Javascriptのオブジェクトの関数呼び出しは文句を言わない
私はすべてで動作するように停止機能を取得しようとしていますすぐに演奏される音。残念なことに、私がbuttonpressからこの関数を呼び出すと、オブジェクトには停止機能がないようです。実際の音要素は以下の通りですのコード:
// Container to keep all sounds in one place. This is a Dictionary within a dictionary to be able to search by catagory.
var sounds = {};
// Counter to keep track of unique ID's
var lastID = 0;
// Base class for initializing Any class
var Base = function(methods){
var base = function() {
this.initialize.apply(this, arguments);
};
for (var property in methods) {
base.prototype[property] = methods[property];
}
if (!base.prototype.initialize) base.prototype.initialize = function(){};
return base;
};
//Complete class for the Sound object. Generates its own DIV's and HTML5 tags to play stuff.
var Sound = Base({
// Init all the variables.
initialize: function(name, file, target='Sounds') {
this.name = name;
this.file = file
this.button = null;
this.audioelement;
this.id = lastID + 1;
this.target = target;
lastID ++;
// Check if the catagory is there, if not: create it with a placeholder object
var catagory = sounds[this.target];
if(catagory == null){
sounds[this.target] = {99:null};
}
sounds[this.target][this.id] = this;
// Call init function
this.init();
},
play : function() {
obj = this
if(obj.audioelement.paused == true){
obj.audioelement.play();
}else{
obj.audioelement.pause();
obj.audioelement.fastSeek(0);
}
},
stop : function(){
obj = this;
obj.audioelement.pause();
},
init : function(){
// Statement for JS class based shenanigans.
obj = this
// Create a button and add some text to it
obj.button = document.createElement("BUTTON");
obj.button.appendChild(document.createTextNode(obj.name));
// Set ID's and names to keep track of this button
obj.button.id = obj.id;
obj.button.name = obj.target;
// Get or create parent element. Used for catagory based display
var el = getOrCreateElement(obj.target)
el.appendChild(obj.button);
// Create audio element and set appropriate settings
obj.audioelement = document.createElement("AUDIO");
obj.audioelement.src = obj.file;
obj.audioelement.name
obj.button.appendChild(obj.audioelement);
// Add function to play/pause to button
obj.button.onclick = buttonClicked;
});
function buttonClicked(){
// Fetch sound from dicionary container using the name and id from the button [SET AT SOUND.INIT()]
var sound = sounds[this.name][this.id];
// Call the play function in [SOUND]
sound.play();
}
そしてSTOPALL機能のために:
function stopAll(){
// Scroll through the entire dictionary
for (var key in sounds){
for (var id in sounds[key]){
// Check if the sound is not a placeholder
if(id == 99){
continue;
}
// Call stop function with fetched object.
var sound = sounds[key][id];
sound.stop();
}
}
}
奇妙なことがあるが、停止機能プレイ機能が動作するようには思えないということではなく、 。オブジェクトにはその特定の機能がないと言われています...
任意のアイデアが浮かび上がります!ベース・オブジェクトのプロトタイプのものを含め、単に方法、より多くのいくつかのプロパティを超える
WM
あなたはいくつかの助けが必要な場合は、再生/停止を行うために使用しているコードを投稿する必要があります。 – loretoparisi
実際の機能はすべてそこにあります。欠落しているのは、関数自体を呼び出すロジックだけです。それを行う関数を追加します –
エラーメッセージがありますか? – inf3rno