Setting the currentTime of an <audio> tag not working?

前端 未结 6 663
生来不讨喜
生来不讨喜 2020-12-31 07:41

I have this audio tag playing in the background, and I\'m able to store the progress in seconds to a cookie. But in no way I\'m able to start the audio from that cookie. (fo

6条回答
  •  情书的邮戳
    2020-12-31 08:29

    what I found in my case is that there is an issue with context somewhere. I initialize audio under the window context but when I try to change currentTime from XMLHttpRequest response it does NOT work. I don't know the answer yet but I'm providing a clue maybe an expert in Javascript will know how to make it work.

    /* initialize this.audio player */
    Audio = function() {
        var that = this;
        // keep track of playback status
        var AudioStatus = {
                isPlaying : false
        };
    
        // define references to this.audio, pulldown menu, play-button, slider and time display
        that.audio = document.querySelector("AUDIO");
    
        /* load track by menu-index */
        var loadTrack = function() {
    
            if(that.audio == null){
                log("audio null"); return;
            }
            that.audio.src = '../sounds/400.mp3';
            that.audio.load();
        };
    
        /* callback to play or pause */
        that._play = function() {
            if(that.audio == null){
                log("audio null"); return;
            }  
            that.audio.play();
            AudioStatus.isPlaying = true;
        };
    
        that._pause = function() {
    
            if(that.audio == null){
                log("audio null"); return;
            }
            that.audio.pause();
            AudioStatus.isPlaying = false;
        };
    
        that.playPause = function() {
            if (that.audio.paused) {
                self._play();
            }
            else {
                self._pause();
            }
        };
    
        /* callback to set or update playback position */
        that.updateProgress = function(value) {
            if(that.audio == null){
                log("audio null"); return;
            }
            that.audio.currentTime = value;    // <<<--- it does NOT work if I call from XMLHttpRequest response but it DOES if it is called from a timer expired call back
        };
    
        that.isAudioPlaying = function(){
            return AudioStatus.isPlaying;
        };
    };
    

提交回复
热议问题