获取 YouTube 直播活动的当前持续时间 [英] Get current duration of YouTube Live Event

查看:18
本文介绍了获取 YouTube 直播活动的当前持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在直播到 YouTube 时,有没有办法获取录制流的当前时间?我希望能够在整个实时流中的某些点发送 API 请求,以获取流的当前分钟/秒.我想要达到的最终结果是能够记录一个亮点列表.本质上,用户按下一个按钮,它会获得当时流的当前时间,然后用户可以为当时发生的事情添加注释.但是,通过阅读所有文档,我找不到获取录制流的当前时间的方法.

Is there a way to get the current time of a the recorded stream when broadcasting to YouTube live? I want to be able to send an API request at certain points throughout a live stream to get the current minute/second of the stream. The end result I am trying to achieve is to be able to log a list of highlights. Essentially, a user presses a button and it gets the current time of the stream at that moment, then the user can add a note for what happened at that time. From reading all the docs though, I cannot find a way to get the current time of the recorded stream.

推荐答案

看起来你可以使用 iFrame API 的 getDuration() 方法来做到这一点.

Looks like you can do this with the iFrame API's getDuration() method.

https://developers.google.com/youtube/iframe_api_reference#getDuration

查看现场活动的特别说明:

Check out the special note for live events:

如果当前播放的视频是直播事件,getDuration() 函数将返回直播视频流开始后经过的时间.具体来说,这是视频在没有被重置或中断的情况下流式传输的时间量.此外,此持续时间通常比实际活动时间长,因为流式传输可能在活动开始时间之前开始.

If the currently playing video is a live event, the getDuration() function will return the elapsed time since the live video stream began. Specifically, this is the amount of time that the video has streamed without being reset or interrupted. In addition, this duration is commonly longer than the actual event time since streaming may begin before the event's start time.

您没有指定语言,所以我将发布两种不同语言的代码示例.两者都使用 iFrame API.

You didn't specify a language, so I'll post code examples in two different languages. Both utilize the iFrame API.

JavaScript:

JavaScript:

window.onYouTubePlayerReady = function(playerId) {
    window.ytplayer = document.getElementById("ytPlayer");
    console.log(window.ytplayer.getDuration());
}

Objective-C(使用 YouTube 的 youtube-ios-player-helper 类)

Objective-C (using YouTube's youtube-ios-player-helper class)

@property (weak, nonatomic) IBOutlet YTPlayerView *playerView;

// ...

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self.playerView loadWithVideoId:@"iGTIK_8ydoI"] // live at the time answer was posted
}

// ...

- (void)getDurationOfPlayingVideo {

    NSLog(@"duration: %d", [self.playerView duration]);
}

正如我个人测试中的免责声明:Live Streaming API 非常易怒且不稳定,我发现一些 Live Event 返回的持续时间为 0.

Just as a disclaimer from my personal testing: the Live Streaming API is extraordinary temperamental and unstable, and I've found that some Live Events return a duration of 0.

这篇关于获取 YouTube 直播活动的当前持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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