Get current duration of YouTube Live Event

前端 未结 2 1110
执笔经年
执笔经年 2020-12-20 01:51

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 li

相关标签:
2条回答
  • this is old but you can get the liveStreamingDetails.actualStartTime through the youtube API.
    With the actualStartTime in hands, you can calculate how much time elapsed.

    "https://www.googleapis.com/youtube/v3/videos"
            "?part=liveStreamingDetails"
            "&id=$id&key=$_key"
    
    0 讨论(0)
  • 2020-12-20 02:30

    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:

    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.

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

    JavaScript:

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

    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]);
    }
    

    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.

    0 讨论(0)
提交回复
热议问题