【发布时间】:2013-07-12 18:01:44
【问题描述】:
我正在尝试使用 .NET Mirror API 附加视频流,但遇到了一些问题。
我似乎找不到支持此处引用的格式的方法:
POST /upload/mirror/v1/timeline HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer {auth token}
Content-Type: multipart/related; boundary="mymultipartboundary"
Content-Length: {length}
--mymultipartboundary
Content-Type: application/json; charset=UTF-8
{ "text": "Skateboarding kittens" }
--mymultipartboundary
Content-Type: video/vnd.google-glass.stream-url
http://example.com/path/to/kittens.mp4
--mymultipartboundary--
我从谷歌看到的最好的信息是使用以下方法进行插入:
/// <summary>
/// Insert a new timeline item in the user's glass with an optional
/// notification and attachment.
/// </summary>
/// <param name='service'>Authorized Mirror service.</param>
/// <param name='text'>Timeline Item's text.</param>
/// <param name='contentType'>
/// Optional attachment's content type (supported content types are
/// "image/*", "video/*" and "audio/*").
/// </param>
/// <param name='attachment'>Optional attachment stream</param>
/// <param name='notificationLevel'>
/// Optional notification level, supported values are null and
/// "AUDIO_ONLY".
/// </param>
/// <returns>
/// Inserted timeline item on success, null otherwise.
/// </returns>
public static TimelineItem InsertTimelineItem(MirrorService service,
String text, String contentType, Stream attachment,
String notificationLevel) {
TimelineItem timelineItem = new TimelineItem();
timelineItem.Text = text;
if (!String.IsNullOrEmpty(notificationLevel)) {
timelineItem.Notification = new NotificationConfig() {
Level = notificationLevel
};
}
try {
if (!String.IsNullOrEmpty(contentType) && attachment != null) {
// Insert both metadata and media.
TimelineResource.InsertMediaUpload request = service.Timeline.Insert(
timelineItem, attachment, contentType);
request.Upload();
return request.ResponseBody;
} else {
// Insert metadata only.
return service.Timeline.Insert(timelineItem).Fetch();
}
} catch (Exception e) {
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
但是,此代码将内容作为流“附加”(这对于上传图像非常有用,我已经测试过并且可以使用)。但是,流式视频只需要视频的 URL。
我尝试将 URL 的字符串表示形式作为流发送,但结果只是一个无限加载的视频。
我已经成功地通过使用我的身份验证令牌和上面的 POST 请求发出 cURL 请求来播放视频,所以我知道视频本身不是问题。
有没有人能够让流视频通过 .NET(使用 Mirror API 或使用某种自定义 WebRequest?)我尝试自己从头开始创建 WebRequest,但我得到了 400 个一个回应。
作为参考,我尝试过的其他代码:
var request = WebRequest.CreateHttp(baseAddress + method);
request.Method = "POST";
request.Headers.Add("Authorization", string.Format("Bearer {0}", auth));
string itemJson = JsonConvert.SerializeObject(item.Item, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
string contentFormat = "--MyBound\nContent-Type: application/json; charset=UTF-8\n\n{0}\n--MyBound\nContent-Type: video/vnd.google-glass.stream-url\n\n{1}\n--MyBound--";
string content = string.Format(contentFormat, new[] { itemJson, item.VideoUrl });
request.ContentLength = content.Length;
request.ContentType = "multipart/related; boundary=\"MyBound\"";
var rs = request.GetRequestStream();
using (var sw = new StreamWriter(rs))
{
sw.Write(content);
}
var response = request.GetResponse();
其中 item 是我编写的一个类,其中包含作为字符串的 VideoUrl 和 Item(来自 Mirror API 的 TimelineItem),而我正在使用的视频 Url 是:
http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
提前谢谢大家!
【问题讨论】:
标签: google-mirror-api google-glass