我在Java中使用YouTube Live Streaming API。
我有一个问题,有时会发生。

我编写了一个类,以检索现有的实时流并创建实时广播,确保该流正在发送数据,然后将它们绑定在一起,然后发出2个转换请求。

首先,我过渡到“测试”(实时预览)请求,然后过渡到“实时”请求,
https://developers.google.com/youtube/v3/live/life-of-a-broadcast第4.1节的手册说
我应该轮询API,直到广播生命周期状态变为实时为止

这是我写的:
'''

 YouTube.LiveBroadcasts.Transition requestLive = CreateYouTube.getYoutube().liveBroadcasts()
                    .transition("live", returnedBroadcast.getId(), "snippet,status");
            returnedBroadcast = requestLive.execute();
            //poll while live starting (wait while starting live)
  while(returnedBroadcast.getStatus().getLifeCycleStatus().equals("liveStarting")) {
           returnedBroadcast = getBroadcastById(returnedBroadcast.getId());
           System.out.println("polling liveStarting "+args[0]);
           Thread.sleep(1000);
 }


'''
有时它工作得很顺利,但是在其他时候状态不会改变,这意味着广播不会转换为直播,并且我陷入了无限循环。
当然,我可以手动跳出循环,但我仍然不明白为什么未处理请求,我可以解决它。

最佳答案

为什么要查找状态liveStarting而不是live?超时可能会变得很不幸,并且会完全丢失状态。 liveStarting是过渡状态。

https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.lifeCycleStatus

我还建议查看contentDetails.enableAutoStart资源上的LiveBroadcast属性。

可以在附加LiveBroadcast之前将enableAutoStart: true更新为LiveStream。我发现这是启动流的更可靠的方法。

https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#contentDetails.enableAutoStart

10-06 04:54
查看更多