Player.seekTo 不是一个函数

问题描述 投票:0回答:2

我有以下初始化:

Player.init(element.id, playerId, () => {
    this.onReady(videoId, socket)
})

和回调:

onReady(videoId, socket){
    let msgContainer = document.getElementById("msg-container")
    let msgInput     = document.getElementById("msg-input")
    let postButton   = document.getElementById("msg-submit")
    let vidChannel   = socket.channel("videos:" + videoId)

    postButton.addEventListener("click", e => {
        let payload = {body: msgInput.value, at: Player.getCurrentTime()}
        vidChannel.push("new_annotation", payload)
            .receive("error", e => console.log(e) )
        msgInput.value = ""
    })

    msgContainer.addEventListener("click", e => {
        e.preventDefault()
        let seconds = e.target.getAttribute("data-seek") ||
                      e.target.parentNode.getAttribute("data-seek")
        if (!seconds) { return }

        Player.seekTo(seconds)
    })

    ...
}

然而,Player.seekTo 函数失败并显示

_player2.default.seekTo is not a function
,而 Player.getCurrentTime() 函数调用则按预期工作。

编辑:我也尝试过使用eekahead参数,结果相同。

youtube-api youtube-iframe-api
2个回答
1
投票

从 YouTube 的 documentation 来看,

player.seekTo(seconds:Number, allowSeekAhead:Boolean)
有两个参数需要设置。

  • seconds
    参数标识玩家应该前进的时间。

还有

  • allowSeekAhead
    参数决定如果
    seconds
    参数指定的时间在当前缓冲的视频数据之外,播放器是否会向服务器发出新请求。

所以你的代码必须如下所示:

msgContainer.addEventListener("click", e => {
    e.preventDefault()
    let seconds = e.target.getAttribute("data-seek") ||
    e.target.parentNode.getAttribute("data-seek")
    if (!seconds) { return }

    Player.seekTo(120, true)//120 seconds
})

有关更多信息,请检查这个问题:


0
投票

我知道这是一个老问题,但我也为此苦苦挣扎。 这是我遇到的情况。

在我的 HTML 代码中,我有

<iframe id="player" type="text/html"
    width="640"
    height="390"
    src="http://www.youtube.com/embed/...?enablejsapi=1&origin=https://[my_domain].com/"
    frameborder="1"
></iframe>

使用 src 属性的

&origin= ...
部分,我遇到了相同的错误 -
Player.seekTo()
不是函数。 如果没有
&origin= ...
参数,我的代码就会运行。

我知道原始部分是一项安全功能,但您可以在调试时尝试使用或不使用该参数。

© www.soinside.com 2019 - 2024. All rights reserved.