通过 API 启动 YouTube 直播

我遇到了 YouTube API 的问题,我正在使用 ("google/apiclient": "2.7")

我已经创建了广播并将其绑定到流,然后将 RTMP URL 作为端点添加到我的直播中但我找不到在 YouTube 上启动直播的方法(在原始直播开始后)

   $access_token = $data['yt-access-token'];

        $title = $data['title'];

        $description = $data['description'];

        //=======================================//

        $client = new Google_Client();

        $client->setClientId(env('GOOGLE_APP_ID'));

        $client->setClientSecret(env('GOOGLE_SECRET'));

        $client->setScopes('https://www.googleapis.com/auth/youtube');

        $client->setAccessToken($access_token);


        // Define an object that will be used to make all API requests.

        $youtube = new Google_Service_YouTube($client);

        //=======================================//

        try {

            // Create an object for the liveBroadcast resource's snippet. Specify values

            // for the snippet's title, scheduled start time, and scheduled end time.

            $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();

            $broadcastSnippet->setTitle($title);

            $broadcastSnippet->setDescription($description);

            $broadcastSnippet->setScheduledStartTime('2020-08-20T00:00:00.000Z');

            $broadcastSnippet->setScheduledEndTime('2020-08-25T00:00:00.000Z');

        

            // Create an object for the liveBroadcast resource's status, and set the

            // broadcast's status to "private".

            $status = new Google_Service_YouTube_LiveBroadcastStatus();

            $status->setPrivacyStatus('public'); //private or public

        

            // Create the API request that inserts the liveBroadcast resource.

            $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();

            $broadcastInsert->setSnippet($broadcastSnippet);

            $broadcastInsert->setStatus($status);

            $broadcastInsert->setKind('youtube#liveBroadcast');


                )

            );

我在 YouTube 管理室中找到了这些选项,但在 API 中找不到它们


https://img1.sycdn.imooc.com/652ba78d000131c606140512.jpg

有什么解决办法吗?



守着星空守着你
浏览 134回答 0
0回答

慕哥6287543

根据文档,您可以使用以下两个属性LiveBroadcasts resource:contentDetails.enableAutoStart (boolean)指示当您在绑定的直播流上开始流式传输视频时,是否应自动开始此广播。contentDetails.enableAutoStop (boolean)指示此广播是否应在频道所有者停止在绑定视频流上流式传输视频后一分钟左右自动停止。LiveBroadscasts.insert允许通过两个修改端点和随意设置这两个属性Livebroadcasts.update。对于您的代码,您必须执行以下操作:$contentDetails = new Google_Service_YouTube_LiveBroadcastContentDetails();$contentDetails->setEnableAutoStart(true);$contentDetails->setEnableAutoStop(true);然后还有:$broadcastInsert->setContentDetails($contentDetails);并将调用替换为$youtube->liveBroadcasts->insert:$broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status,contentDetails', $broadcastInsert, array());
打开App,查看更多内容
随时随地看视频慕课网APP