我的应用可以访问YouTube数据API v3,但是我不能正确使用直播功能。这是我的参数配置错误吗?
我的代码在一月份仍然可以正常运行,但是不久前我发现我在现场直播时无法在YouTube上实时看到它。我花了一个小时或两个小时才能观看直播视频。Google平台是否有变化?
private List<String> getDataFromApi() throws IOException {
// Get a list of up to 10 files.
List<String> channelInfo = new ArrayList<String>();
ChannelListResponse result = mService.channels().list("snippet,contentDetails,statistics")
.setForUsername("GoogleDevelopers")
.execute();
List<Channel> channels = result.getItems();
if (channels != null) {
Channel channel = channels.get(0);
channelInfo.add("This channel's ID is " + channel.getId() + ". " +
"Its title is '" + channel.getSnippet().getTitle() + ", " +
"and it has " + channel.getStatistics().getViewCount() + " views.");
}
LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
broadcastSnippet.setTitle("GoLive Cam Test");
String format="yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setTimeZone(TimeZone.getTimeZone("GMT-8"));//指定时区
String dateStr = sdf.format(Calendar.getInstance().getTime());
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = null;
long time1 = 0L;
try {
date = formatter.parse(dateStr);
time1 = date.getTime()+1*60*1000;
} catch (ParseException e) {
e.printStackTrace();
}
long time = System.currentTimeMillis()+3*60*1000;
broadcastSnippet.setPublishedAt(new DateTime(time1));
broadcastSnippet.setActualStartTime(new DateTime(time));
broadcastSnippet.setScheduledStartTime(new DateTime(time));
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.setPrivacyStatus("public");
LiveBroadcast broadcast = new LiveBroadcast();
broadcast.setKind("youtube#liveBroadcast");
broadcast.setSnippet(broadcastSnippet);
broadcast.setStatus(status);
LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
contentDetails.setProjection("360");
broadcast.setContentDetails(contentDetails);
YouTube.LiveBroadcasts.Insert liveBroadcastInsert =
mService.liveBroadcasts().insert("snippet,status,contentDetails", broadcast);
broadcast = liveBroadcastInsert.execute();
LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
streamSnippet.setTitle("GoLive Cam Test");
IngestionInfo ingestionInfo = new IngestionInfo();
ingestionInfo.setStreamName("GoLive Stream");
ingestionInfo.setIngestionAddress("rtmp://a.rtmp.youtube.com/live2");
CdnSettings cdnSettings = new CdnSettings();
cdnSettings.setIngestionInfo(ingestionInfo);
cdnSettings.setResolution("720P");
cdnSettings.setFrameRate("30fps");
cdnSettings.setIngestionType("rtmp");
LiveStream stream = new LiveStream();
stream.setKind("youtube#liveStream");
stream.setSnippet(streamSnippet);
stream.setCdn(cdnSettings);
YouTube.LiveStreams.Insert liveStreamInsert =
mService.liveStreams().insert("snippet,cdn", stream);
stream = liveStreamInsert.execute();
push_addr = stream.getCdn().getIngestionInfo().getIngestionAddress() + "/" + stream.getCdn()
.getIngestionInfo().getStreamName();
handler.sendEmptyMessage(0);
YouTube.LiveBroadcasts.Bind liveBroadcastBind =
mService.liveBroadcasts().bind(broadcast.getId(), "id,contentDetails");
liveBroadcastBind.setStreamId(stream.getId());
broadcast = liveBroadcastBind.execute();
String share_addr = "https://www.youtube.com/watch?v=" + broadcast.getId();
YouTube.LiveStreams.List
liveStreamRequest = mService.liveStreams()
.list("id,status")
.setId(broadcast.getContentDetails()
.getBoundStreamId());
LiveStreamListResponse returnedList = liveStreamRequest.execute();
List<LiveStream> liveStreams = returnedList.getItems();
if (liveStreams != null && liveStreams.size() > 0) {
LiveStream liveStream = liveStreams.get(0);
if (liveStream != null)
while (!liveStream.getStatus().getStreamStatus().equals("active")) {
try {
Thread.sleep(1000);
returnedList = liveStreamRequest.execute();
liveStreams = returnedList.getItems();
liveStream = liveStreams.get(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
final YouTube.LiveBroadcasts.Transition broadCastTestingRequest = mService
.liveBroadcasts().transition("testing",
broadcast.getId(), "id,snippet,contentDetails,status");
broadcast = broadCastTestingRequest.execute();
handler.sendEmptyMessage(1);
YouTube.LiveBroadcasts.List liveBroadRequest = mService.liveBroadcasts().list("id,status");
liveBroadRequest.setBroadcastStatus("all");
LiveBroadcastListResponse liveBroadcastResponse = liveBroadRequest.execute();
List<LiveBroadcast> returnedList1 = liveBroadcastResponse.getItems();
if (returnedList1 != null && returnedList1.size() > 0) {
LiveBroadcast liveBroadcastReq = returnedList1.get(0);
if (liveBroadcastReq != null)
while (!liveBroadcastReq.getStatus().getLifeCycleStatus().equals("testing")) {
try {
Thread.sleep(1000);
AppLog.d("Error","publish broadcast - getLifeCycleStatus: " + liveBroadcastReq.getStatus().getLifeCycleStatus());
liveBroadcastResponse = liveBroadRequest.execute();
returnedList1 = liveBroadcastResponse.getItems();
liveBroadcastReq = returnedList1.get(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
YouTube.LiveBroadcasts.Transition transitionRequest = mService.liveBroadcasts().transition("live", broadcast.getId(), "status");
broadcast = transitionRequest.execute();
try {
Thread.sleep(5000);
Message message = new Message();
message.what = 2 ;
Bundle bundle = new Bundle();
bundle.putString("share_address",share_addr);
message.setData(bundle);
handler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return channelInfo;
}
就像我上面发布的照片一样,花一两个小时才能看到我的实况视频。