使用.net将YouTube视频推送到Glassware TimeLine卡

时间:2013-10-16 06:14:49

标签: google-mirror-api google-glass

我需要使用.Net将YouTube视频链接添加到我的Glassware TimeLine卡。我已经将其添加为流,但它有更多时间上传和播放。

    Service.Timeline.Insert(timelineItem, stream, "video/mp4").Upload();

我可以将其添加为YouTube链接。

谢谢

1 个答案:

答案 0 :(得分:2)

可以在时间轴卡中流式传输YouTube视频,这里是C#.Net代码。使用此名称空间“YoutubeExtractor”。我正在做的是从标准youtube网址解析流网址,然后从中选择视频网址并将其流式传输。这对我来说很好。获取youtube视频网址后,获取选择共享后的链接。

 private static String InsertItem5(MainController controller)
    {

        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;


        TimelineItem critical = new TimelineItem()
        {

            Text = "Menu Card",
            BundleId = "666",
            Notification = new NotificationConfig() { Level = "DEFAULT" },
            MenuItems = new List<MenuItem>()
                                     {
                                         new MenuItem() {Action = "DELETE"},
                                     }

        };

        String mediaLink = vLink;

        if (!String.IsNullOrEmpty(mediaLink))
        {
            Stream stream = null;
            if (mediaLink.StartsWith("/"))
            {
                stream = new StreamReader(controller.Server.MapPath(mediaLink)).BaseStream;
            }
            else
            {
                HttpWebRequest request = WebRequest.Create(mediaLink) 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;
                    do
                    {
                        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(critical, stream, "video/mp4").Upload();
        }
        else
        {
            controller.Service.Timeline.Insert(critical).Fetch();
        }

参考:http://pathofacoder.com/2013/10/16/stream-you-tube-video-in-google-glass-time-line-card-using-mirror-apic-net/