Tag Archives: youtube

Stream You Tube Video, in Google Glass Time Line Card, using Mirror API(C#.net)

Ok guys today we will see how we can stream a Youtube video inside a google glass timeline card.  First you need to add reference to the “YoutubeExtractor” namespace. You can get it from the nuget if you dont have it.

using YoutubeExtractor;

Now add the youtube video url to atring(The url you got when you clicked the share button not the page url).
Resolve the url and get the links for the available videos.
Select the video you wanted to stream and get the stream url.

string link = "http://youtu.be/9uYKISlL7Vg";
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);
String vLink = video.DownloadUrl;

Now create the timeline card and add the menu item

TimelineItem yotubeVideo = new TimelineItem()
Text = "Youtube Video",
BundleId = "8081",
MenuItems = new List<MenuItem>() { { new MenuItem() { Action = "DELETE" } } },
Notification = new NotificationConfig() { Level = "DEFAULT" }

Create a stream using the link we got, create a http request, buffer it and insert the card mentionng the video type.

String videoLink = vLink;

if (!String.IsNullOrEmpty(videoLink))
Stream stream = null;
if (videoLink.StartsWith("/"))
stream = new StreamReader(controller.Server.MapPath(videoLink)).BaseStream;
HttpWebRequest request = WebRequest.Create(videoLink) as HttpWebRequest;

request.UseDefaultCredentials = false;

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

byte[] b = null;
using (Stream streamFromWeb = response.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
int count = 0;
byte[] buf = new byte[1024];
count = streamFromWeb.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (streamFromWeb.CanRead && count > 0);
b = ms.ToArray();

stream = new MemoryStream(b);
controller.Service.Timeline.Insert(yotubeVideo, stream, "video/mp4").Upload();

Now the stream will work like a charm