Youtube API设置当前时间 [英] Youtube API Set current time

查看:212
本文介绍了Youtube API设置当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用You-Tube API作为我的一个项目。
我使用它的方式如下:

  var tag = document.createElement('script'); 
tag.src =https://www.youtube.com/iframe_api;
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag,firstScriptTag);

// 3.此函数创建一个< iframe> (和YouTube播放器)
// API代码下载后。
var player;
var alternatePlayer;
function onYouTubeIframeAPIReady(){
player = new YT.Player('player',{
height:'390',
width:'640',
videoId :'M7lc1UVf-VE',
events:{
'onReady':onPlayerReady
}
});

}

// 4.当视频播放器准备就绪时,API将调用此函数。
function onPlayerReady(event){
event.target.playVideo();
}

这一切运作良好,但对于我的项目,我需要能够控制视频正在播放的当前时间。
我搜索了You-Tube API笔记和数据,但找不到一个参数,也没有一个函数使我能够设置视频的当前时间(当前时间有一个GET,但没有一个设置)。
当创建YouTube时(我的意思是靠近参数height,width,videoId)可以控制视频从哪里开始,参数 startseconds 。这不符合我的需要,因为我需要更改同一视频的当前时间,而不是加载另一个视频。有没有办法解决?我是否缺少这个设置功能?我必须回想一下带有 startseconds 的videoId吗?
非常感谢所有关于此事的信息。






编辑:
好​​的,我以某种奇怪的方式找到了解决方案。出于某种原因,我没有在You-Tube API中看到它。

  player.seekTo(55,true); 

这段代码会将你的管乐器变成55秒,这是当前时间的SET 。
感谢大家的时间。

解决方案

查看以下文档:





首先,您需要对 Player的JS引用对象。为了快速入门,请看下面的问题:


  • 如何获得对现有YouTube播放器的参考?



  • 有了这个参考,你可以做:

      player.seekTo 42); //转42秒


    I am currently using the You-Tube API for a project of mine. The way I use it is the following:

    var tag = document.createElement('script');
        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    // 3. This function creates an <iframe> (and YouTube player)
    //    after the API code downloads.
    var player;
    var alternatePlayer;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '390',
            width: '640',
            videoId: 'M7lc1UVf-VE',
            events: {
                'onReady': onPlayerReady
            }
        });
    
    }
    
    // 4. The API will call this function when the video player is ready.
    function onPlayerReady(event) {
        event.target.playVideo();
    }
    

    It all works well, but for my project I need to be able to control the current time that the video is playing. I have searched the You-Tube API notes and data but could not find a paramter nor a function that enables me to set the current time of the video (There is a GET for the current time but not a set). There is a parameter startseconds that when mentioned in the youtube creation (I mean near the parameters height,width,videoId) can control what second the video starts from. This does not fit my needs because I need to change the current time of the SAME video and not load another one. Is there any way around this? Am I missing this set function? Must I recall the same videoId with the startseconds? Thanks very much in advance for any light on that matter.


    EDIT: Okay, I have found the solution in some weird way. For some reason I don't see it mentioned on the You-Tube API.

    player.seekTo(55,true); 
    

    This code will change the you-tube player to 55 seconds, which is a SET for the current time. Thanks everyone for your time.

    解决方案

    Take a look into the following documents:

    First you need a JS reference to the Player object. For a quick start, please take a look at the following Question:

    With this reference you can do:

    player.seekTo(42); //Go to 42 seconds
    

    这篇关于Youtube API设置当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆