seekTo() is not a function YouTube iframe API error

我的未来我决定 提交于 2020-05-16 02:21:49

问题


I am using a Wordpress plugin to add timestamp links of videos that will seek the video automatically to a certain timeframe.

Javascript:

        function onYouTubeIframeAPIReady(){

    console.log('Confirmation of call to onYouTubeIframeAPIReady()');

    var STT = {
        settings: STTSettings,
        media: undefined,
        skipTo: undefined,
        isHTML5: false,
        isYoutube: true,

        doHTML5Skip: function() {
            STT.media.removeEventListener('canplaythrough', STT.doHTML5Skip);
            STT.media.currentTime = STT.skipTo;
            STT.media.play();
        },

        doYoutubeSkip: function() {

            STT.media.seekTo(STT.skipTo);
            STT.media.playVideo();
        }

    };




    STTSkipTo = function(time) {
        var audio       = document.getElementsByTagName('audio'),
            video       = document.getElementsByTagName('video'),
            iframe      = document.getElementsByTagName('iframe'),
            timeArray   = time.split(':').reverse(),
            seconds     = parseInt(timeArray[0]),
            minutes     = timeArray.length > 1 ? parseInt(timeArray[1]) : 0,
            hours       = timeArray.length > 2 ? parseInt(timeArray[2]) : 0;

        STT.skipTo = seconds + (minutes * 60) + (hours * 3600);

        if (STT.media) {
            console.log(STT.media.seekTo);
            STT.doSkip();

            return;
        }

        if ((parseInt(STT.settings.link_audio) && audio.length) ||
            (parseInt(STT.settings.link_video) && video.length))
        {
            STT.doSkip = STT.doHTML5Skip;

            if (parseInt(STT.settings.link_audio) && audio.length) {
                STT.media = audio[0];
            } else {
                STT.media = video[0];
            }

            STT.media.addEventListener('canplaythrough', STT.doHTML5Skip);
            STT.media.load();
            STT.media.play();
            return;
        } else if (parseInt(STT.settings.link_youtube && iframe.length)) {
            // Inspect the iframes, looking for a src with youtube in the URI
            for (var i = 0; i < iframe.length; i++) {
                if (iframe[i].src.search('youtube') !== -1) {
                    // Set up the JS interface
                    STT.doSkip = STT.doYoutubeSkip;

                    iframe[0].id = 'stt-youtube-player';
                    STT.media = new YT.Player('stt-youtube-player', {
                        events: {
                            onReady: STT.doYoutubeSkip
                        }
                    });
                    return;
                }
            }
        }

        console.log('Skip to Timestamp: No media player found!');
        return;
    }

    }

On my localhost, the plugin works seamlessly but on my hosted website, I get the following error with the stack as follows:

Uncaught TypeError: STT.media.seekTo is not a function

I think for some reason the website is unable to load the www-widgetapi.js which is a dependency for YouTube iframe API and thus is unable to generate the required function definition. However, I did try to include the script manually in the header but it still didn't work.

If anyone knows of any other wordpress plugin, please advice.


回答1:


Based from this documentation, you need to set both the two parameter of the player.seekTo(seconds:Number, allowSeekAhead:Boolean).

Seeks to a specified time in the video. If the player is paused when the function is called, it will remain paused. If the function is called from another state (playing, video cued, etc.), the player will play the video.

  • The seconds parameter identifies the time to which the player should advance.

    The player will advance to the closest keyframe before that time unless the player has already downloaded the portion of the video to which the user is seeking.

  • The allowSeekAhead parameter determines whether the player will make a new request to the server if the seconds parameter specifies a time outside of the currently buffered video data.

It should be like: Player.seekTo(120, true)//120 seconds



来源:https://stackoverflow.com/questions/43216661/seekto-is-not-a-function-youtube-iframe-api-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!