Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未调用而触发的类方法_Javascript_Ios_Html5 Audio - Fatal编程技术网

Javascript 未调用而触发的类方法

Javascript 未调用而触发的类方法,javascript,ios,html5-audio,Javascript,Ios,Html5 Audio,我定义了一个类来处理播放音频文件。我正在实例化这个类,并调用它的addEventListener()方法,此时,playSound()被触发,而没有点击元素。另外,当我调用getEventListeners(bgMusic.elem)时,监听器不再连接 class WebAudio { constructor(soundFile, elem) { this.soundFile = soundFile; this.elem = elem;

我定义了一个类来处理播放音频文件。我正在实例化这个类,并调用它的
addEventListener()
方法,此时,
playSound()
被触发,而没有点击元素。另外,当我调用
getEventListeners(bgMusic.elem)
时,监听器不再连接

class WebAudio {

    constructor(soundFile, elem) {
        this.soundFile = soundFile;
        this.elem = elem;
        this.audio = new Audio('sound/' + this.soundFile);
    }

    addListener() {
        this.elem.addEventListener('touchstart', this.playSound());
    }

    playSound() {
        if (context.state != 'suspended') {
            console.log('playing audio file');
            if (!this.audio.playing) {
                this.audio.play();
            }
        } else {
            console.log("Audio Context locked? " + context.state)
        }
    }

}

var AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
function webAudioTouchUnlock(context) {
    return new Promise( (resolve, reject) => {
        //if AudioContext is suspended, and window has been interacted with
        if (context.state === 'suspended' && 'ontouchstart' in window) {
           console.log(context.state);
           var unlock = () => {
               //resume AudioContext (allow playing sound), remove event listeners
               context.resume().then(() => {
                   console.log("context resumed");
                   document.body.removeEventListener('touchstart', unlock);
                   document.body.removeEventListener('touchend', unlock);
                   resolve(true);
               }, function (reason) {
                 reject(reason);
               });
           };
           document.body.addEventListener('touchstart', unlock, false);
           document.body.addEventListener('touchend', unlock, false);
       } else {
           console.log('context not suspended? Context is ' + context.state);
           resolve(false);
       }
    });
}

webAudioTouchUnlock(context);
let bgMusic = new WebAudio('bensound-clearday.mp3', document.querySelector('#sound_button'));
bgMusic.addListener();

添加事件侦听器时,如:

 this.elem.addEventListener('touchstart', this.playSound());
您需要调用函数:
this.playSound()
并添加该函数的结果(
undefined
)作为侦听器

您只需要将引用添加到函数:

this.elem.addEventListener('touchstart', this.playSound);
因此,侦听器也可以在需要时调用它

此外,您可能需要使用类似的方法来维护正确的
this

this.elem.addEventListener('touchstart', () => this.playSound());
或:

this.elem.addEventListener('touchstart', this.playSound.bind(this));