且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取***直播活动的当前持续时间

更新时间:2023-01-27 15:44:59

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

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

https://developers.google.com/***/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.on***PlayerReady = function(playerId) {
    window.ytplayer = document.getElementById("ytPlayer");
    console.log(window.ytplayer.getDuration());
}

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

Objective-C (using ***'s ***-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 Events返回的持续时间为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.