如何获取特定直播活动的rtmp网址和流名称?
以前我一直在使用Youtube API v.2.0,可以检索包含rtmp网址和流名称的直播活动列表。使用新的Youtube Live Streaming API(v.3.0),我可以使用liveBroadcasts
list方法检索实时事件列表,但响应中不包含任何rtmp URL和流名称。为了获得它们,我应该创建一个新的liveStream
资源或使用现有资源。
有没有办法获得rtmp网址和流名称而不这样做?
答案 0 :(得分:3)
你是对的,你必须创建liveStream。广播只是事件对象,而流是您的连接点。 以下是一些示例:https://developers.google.com/youtube/v3/live/docs/liveBroadcasts/insert#examples
答案 1 :(得分:0)
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/youtube.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
gapi.client.setApiKey("YOUR_API_KEY");
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.youtube.liveStreams.list({
"part": [
"snippet,cdn,contentDetails,status"
//"cdn"
],
"mine": true
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
var responseData = JSON.stringify(response);
alert(responseData);
//alert(response.result.items);
var itemsArr = response.result.items;
var itemObj = itemsArr[0];
alert('streamName = ' + itemObj.cdn.ingestionInfo.streamName);
//alert(responseData.result);
//var result = responseData.result;
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
});
<script src="https://apis.google.com/js/api.js"></script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>