Javascript 在Youtube API中启用实时流媒体

Javascript 在Youtube API中启用实时流媒体,javascript,php,youtube-api,youtube-livestreaming-api,Javascript,Php,Youtube Api,Youtube Livestreaming Api,我正在使用Youtube API从我在Youtube中的应用程序中创建事件,用于实时流媒体。这通过以下代码成功完成。但我的要求是: 1) 我需要使用youtube从我的应用程序界面启动实时流 有可能吗 2) 当我使用youtube API从应用程序创建事件时,它会自动转到自定义(更多编码选项)类型。我需要在Quick(使用Google+Hangouts On Air)类型中创建实时流媒体事件。可以吗 我的代码: <?php // Call set_include_path() as ne

我正在使用Youtube API从我在Youtube中的应用程序中创建事件,用于实时流媒体。这通过以下代码成功完成。但我的要求是:

1) 我需要使用youtube从我的应用程序界面启动实时流 有可能吗

2) 当我使用youtube API从应用程序创建事件时,它会自动转到
自定义(更多编码选项)
类型。我需要在
Quick(使用Google+Hangouts On Air)
类型中创建实时流媒体事件。可以吗

我的代码:

<?php

// Call set_include_path() as needed to point to your client library.
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();
//'2015-08-28T00:00:00.000Z'
//'2015-08-29T00:00:00.000Z'
/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
 ?>
 <script type="text/javascript">
    var datefield=document.createElement("input")
    datefield.setAttribute("type", "date")
    if (datefield.type!="date"){ //if browser doesn't support input type="date", load files for jQuery UI Date Picker
        document.write('<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"><\/script>\n')
        document.write('<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"><\/script>\n') 
    }
</script>

<script>
if (datefield.type!="date"){ //if browser doesn't support input type="date", initialize date picker widget:
    jQuery(function($){ //on document.ready
        $('.dateStart').datepicker();
        $('.dateEnd').datepicker();
    })
}
</script>

 <?php

$OAUTH2_CLIENT_ID = 'dsfsfsdfofgm9e4uvg.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'dsfsdfdsfPhf55tcCDLu';
$htmlBody="";
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}


// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
if(isset($_POST['title']) && isset($_POST['timeStart']) && isset($_POST['timeEnd']) && isset($_POST['Status']) && isset($_POST['dateStart']) && isset($_POST['dateEnd']))
{
  try {

    // Create an object for the liveBroadcast resource's snippet. Specify values
    // for the snippet's title, scheduled start time, and scheduled end time.
    $startTime  = $_POST['dateStart']."T".$_POST['timeStart'].":00+0530";
    $endTime  = $_POST['dateEnd']."T".$_POST['timeEnd'].":00+0530";
    //$startDatetime = new DateTime($startTime);
    //$endDatetime = new DateTime($endTime);

    //$startDatetime = $startDatetime->format(DateTime::ISO8601);
    //$endDatetime = $endDatetime->format(DateTime::ISO8601);
    $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
    $broadcastSnippet->setTitle($_POST['title']);
    $broadcastSnippet->setScheduledStartTime($startTime);
    $broadcastSnippet->setScheduledEndTime($endTime);

    // 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($_POST['Status']);

    // 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');

    // Execute the request and return an object that contains information
    // about the new broadcast.
    $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status',
        $broadcastInsert, array());

    // Create an object for the liveStream resource's snippet. Specify a value
    // for the snippet's title.
    $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
    $streamSnippet->setTitle('New Stream');

    // Create an object for content distribution network details for the live
    // stream and specify the stream's format and ingestion type.
    $cdn = new Google_Service_YouTube_CdnSettings();
    $cdn->setFormat("1080p");
    $cdn->setIngestionType('rtmp');

    // Create the API request that inserts the liveStream resource.
    $streamInsert = new Google_Service_YouTube_LiveStream();
    $streamInsert->setSnippet($streamSnippet);
    $streamInsert->setCdn($cdn);
    $streamInsert->setKind('youtube#liveStream');

    // Execute the request and return an object that contains information
    // about the new stream.
    $streamsResponse = $youtube->liveStreams->insert('snippet,cdn',
        $streamInsert, array());

    // Bind the broadcast to the live stream.
    $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
        $broadcastsResponse['id'],'id,contentDetails',
        array(
            'streamId' => $streamsResponse['id'],
        ));

    $htmlBody .= "<h3>Your Broadcast success fully scheduled.</h3><ul>";
   $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
        $broadcastsResponse['snippet']['title'],
        $broadcastsResponse['snippet']['publishedAt'],
        $broadcastsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Added Stream</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $streamsResponse['snippet']['title'],
        $streamsResponse['id']);
    $htmlBody .= '</ul>';
    $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
        $bindBroadcastResponse['id'],
        $bindBroadcastResponse['contentDetails']['boundStreamId']);
    $htmlBody .= '</ul>';
    //$htmlBody .="<h3>Live  Broadcast</h3><div>";

    //$htmlBody .= sprintf("<iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1'  frameborder='0'></iframe>",
        //  $broadcastsResponse['id']);
    $htmlBody .= '</div>';


  } catch (Google_Service_Exception $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
      // echo $e->getMessage();
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
        htmlspecialchars($e->getMessage()));
  }

  $_SESSION['token'] = $client->getAccessToken();
  } else {
  if(isset($_GET['propid']))
  {
  $projectname = $_GET['name'];
  $htmlBody = <<<END
   <form action="" method="post">
  <label>Event Title:  $projectname</label>
<input type="hidden" name="title" value="$projectname"/><br/><br/>
 <label>Start Time:</label>
<input type="date" name="dateStart"/>
 <select name="timeStart">
  <option value="00:00">12:00 AM</option>
  <option value="00:30">12:30 AM</option>
  <option value="01:00">01 AM</option>
  <option value="01:30">01:30 AM</option>
  <option value="02:00">02:00 AM</option>
  <option value="02:30">02:30 AM</option>
  <option value="03:00">03:00 AM</option>
  <option value="03:30">03:30 AM</option>
  <option value="04:00">04:00 AM</option>
  <option value="04:30">04:30 AM</option>
  <option value="05:00">05:00 AM</option>
  <option value="05:30">05:30 AM</option>
  <option value="06:00">06:00 AM</option>
  <option value="06:30">06:30 AM</option>
  <option value="07:00">07:00 AM</option>
  <option value="07:30">07:30 AM</option>
  <option value="08:00">08:00 AM</option>
  <option value="08:30">08:30 AM</option>
  <option value="09:00">09:00 AM</option>
  <option value="09:30">09:30 AM</option>
  <option value="10:00">10:00 AM</option>
  <option value="10:30">10:30 AM</option>
  <option value="11:00">11:00 AM</option>
  <option value="11:30">11:30 AM</option>
  <option value="12:00">12:00 PM</option>
  <option value="12:30">12:30 PM</option>
  <option value="13:00">01:00 PM</option>
  <option value="13:30">01:30 PM</option>
  <option value="14:00">02:00 PM</option>
  <option value="14:30">02:30 PM</option>
  <option value="15:00">03:00 PM</option>
  <option value="15:30">03:30 PM</option>
  <option value="16:00">04:00 PM</option>
  <option value="16:30">04:30 PM</option>
  <option value="17:00">5:00 PM</option>
  <option value="17:30">5:30 PM</option>
  <option value="18:00">6:00 PM</option>
  <option value="18:30">6:30 PM</option>
  <option value="19:00">7:00 PM</option>
  <option value="19:30">7:30 PM</option>
  <option value="20:00">8:00 PM</option>
  <option value="20:30">8:30 PM</option>
  <option value="21:00">9:00 PM</option>
  <option value="21:30">9:30 PM</option>
  <option value="22:00">10:00 PM</option>
  <option value="22:30">10:30 PM</option>
  <option value="23:00">11:00 PM</option>
  <option value="23:30">11:30 PM</option>
  </select>
<br/><br/>
 <label>End Time:</label>
<input type="date" name="dateEnd"/>
 <select name="timeEnd">
  <option value="00:00">12:00 AM</option>
  <option value="00:30">12:30 AM</option>
  <option value="01:00">01 AM</option>
  <option value="01:30">01:30 AM</option>
  <option value="02:00">02:00 AM</option>
  <option value="02:30">02:30 AM</option>
  <option value="03:00">03:00 AM</option>
  <option value="03:30">03:30 AM</option>
  <option value="04:00">04:00 AM</option>
  <option value="04:30">04:30 AM</option>
  <option value="05:00">05:00 AM</option>
  <option value="05:30">05:30 AM</option>
  <option value="06:00">06:00 AM</option>
  <option value="06:30">06:30 AM</option>
  <option value="07:00">07:00 AM</option>
  <option value="07:30">07:30 AM</option>
  <option value="08:00">08:00 AM</option>
  <option value="08:30">08:30 AM</option>
  <option value="09:00">09:00 AM</option>
  <option value="09:30">09:30 AM</option>
  <option value="10:00">10:00 AM</option>
  <option value="10:30">10:30 AM</option>
  <option value="11:00">11:00 AM</option>
  <option value="11:30">11:30 AM</option>
  <option value="12:00">12:00 PM</option>
  <option value="12:30">12:30 PM</option>
  <option value="13:00">01:00 PM</option>
  <option value="13:30">01:30 PM</option>
  <option value="14:00">02:00 PM</option>
  <option value="14:30">02:30 PM</option>
  <option value="15:00">03:00 PM</option>
  <option value="15:30">03:30 PM</option>
  <option value="16:00">04:00 PM</option>
  <option value="16:30">04:30 PM</option>
  <option value="17:00">5:00 PM</option>
  <option value="17:30">5:30 PM</option>
  <option value="18:00">6:00 PM</option>
  <option value="18:30">6:30 PM</option>
  <option value="19:00">7:00 PM</option>
  <option value="19:30">7:30 PM</option>
  <option value="20:00">8:00 PM</option>
  <option value="20:30">8:30 PM</option>
  <option value="21:00">9:00 PM</option>
  <option value="21:30">9:30 PM</option>
  <option value="22:00">10:00 PM</option>
  <option value="22:30">10:30 PM</option>
  <option value="23:00">11:00 PM</option>
  <option value="23:30">11:30 PM</option>
  </select>
<br/><br/>
<input type="hidden" name="Status" value="unlisted"/>
 <!--<label>Stream Status:</label>
 <select name="Status">
  <option value="public">Public</option>
  <option value="unlisted">Unlisted</option>
  <option value="private">Private</option>
</select> <br/><br/>-->
<label>Participants:</label>
<input type="text" name="emails" placeholder="Comma separated emails"/><br/><br/>
<input type ="submit" value="Submit"/>
</form>
END;
} else {

 $htmlBody = <<<END
 <p>Close this window and click the schedule button for live streaming</p>
<p>You need to <a href="#"  onclick="window.close();">Close Window</a> before proceeding.<p>
END;

}

}
} else {
  // If the user hasn't authorized the app, initiate the OAuth flow
  $state = mt_rand();
  $client->setState($state);
  $_SESSION['state'] = $state;

  $authUrl = $client->createAuthUrl();
  $htmlBody = <<<END
  <h3>Authorization Required</h3>
  <p>You need to <a href="$authUrl" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>Bound Live Broadcast</title>
</head>
<body>

  <?=$htmlBody?>
</body>
</html>
  • 要从应用程序开始播放流媒体,您需要将
    liveBroadcast
    从活动状态转换为实时状态。将
    liveStream
    绑定到
    liveBroadcast
    后,在
    liveBroadcast
    上调用
    transition
    ,从
    测试
    直播

  • 要更改质量选项,请设置
    liveStream
    对象的:

  • 此属性的有效值为:

    • 1080p
    • 1080p_hfr:此值适用于每秒60帧(fps)或高帧速率流
    • 240便士
    • 360便士
    • 480便士
    • 720便士
    • 720p_hfr:此值用于每秒60帧(fps)或高帧速率的流
  • 要从应用程序开始播放流媒体,您需要将
    liveBroadcast
    从活动状态转换为实时状态。将
    liveStream
    绑定到
    liveBroadcast
    后,在
    liveBroadcast
    上调用
    transition
    ,从
    测试
    直播

  • 要更改质量选项,请设置
    liveStream
    对象的:

  • 此属性的有效值为:

    • 1080p
    • 1080p_hfr:此值适用于每秒60帧(fps)或高帧速率流
    • 240便士
    • 360便士
    • 480便士
    • 720便士
    • 720p_hfr:此值用于每秒60帧(fps)或高帧速率的流

    你可以通过使用指定的开始时间创建一个软件,但是我不确定你是否会考虑“启动一个软件”。我相信只有通过YouTube频道而不是API直播时,您才能在空中使用Google+Hangouts。我可以使用API创建事件。是否可以使用YouTube API启用Google+Hangout选项?我认为如果您也使用Google+Hangouts API,您可能可以做到这一点。请参阅和。是否可以将google+hangout API与youtube实时流媒体API结合使用?我认为您可以,但我不太确定您将如何做到这一点。也许你可以从答案中得到一些信息。你可以通过使用指定的开始时间来创建一个软件,但是我不确定你是否会考虑“启动一个软件”。我相信只有通过YouTube频道而不是API直播时,您才能在空中使用Google+Hangouts。我可以使用API创建事件。是否可以使用YouTube API启用Google+Hangout选项?我认为如果您也使用Google+Hangouts API,您可能可以做到这一点。请参阅和。是否可以将google+hangout API与youtube实时流媒体API结合使用?我认为您可以,但我不太确定您将如何做到这一点。也许你可以从答案中得到一些信息。Hello@JAL,我在使用youtube livestream API将我的youtube livestream对象从就绪状态转换为活动状态时遇到问题。我已经创建了一个广播对象和livestream对象,并将它们与正确的响应绑定在一起。但当尝试转换时,会抛出一个错误,说明我的流处于非活动状态。请任何帮助都会有很大的帮助,因为我的工作取决于它。谢谢你Hello@JAL,我在使用youtube livestream API将我的youtube livestream对象从就绪状态转换为活动状态时遇到问题。我已经创建了一个广播对象和livestream对象,并将它们与正确的响应绑定在一起。但当尝试转换时,会抛出一个错误,说明我的流处于非活动状态。请任何帮助都会有很大的帮助,因为我的工作取决于它。非常感谢。