C# Youtube课堂不起作用

C# Youtube课堂不起作用,c#,windows-phone-7,download,C#,Windows Phone 7,Download,我修改了一个用C#编写的Youtube Downloader类,以便在windows phone上使用它。我的结果如下。但是当我调用dw()时;我从web浏览器的e.Result.Read(buffer,0,buffer.Length)行中获得一个文件未找到异常。我当然知道这意味着什么,但我不知道如何修复它。所以我有两个问题:为什么我的代码不起作用?或者有没有其他方法可以在WindowsPhone 7上下载Youtube视频?(如库或免费代码片段…)谢谢 class YoutubeDownloa

我修改了一个用C#编写的Youtube Downloader类,以便在windows phone上使用它。我的结果如下。但是当我调用dw()时;我从web浏览器的e.Result.Read(buffer,0,buffer.Length)行中获得一个文件未找到异常。我当然知道这意味着什么,但我不知道如何修复它。所以我有两个问题:为什么我的代码不起作用?或者有没有其他方法可以在WindowsPhone 7上下载Youtube视频?(如库或免费代码片段…)谢谢

class YoutubeDownload
{
    string youtubeurl;
    string fileext;

    private WebClient webClient = new WebClient();

    public void dw()
    {
        youtubeurl = "http://www.youtube.com/watch?v=locIxsfpgp4&feature=related";
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri(youtubeurl));
    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string rawHtml = e.Result;
        string getUrl = "http://www.youtube.com/get_video.php?video_id={0}&t={1}";
        Regex _titleRegex = new Regex("'VIDEO_TITLE': '(.+)',");
        Regex _qualiRegex = new Regex("\"fmt_map\": \"([0-9]{2})");
        Regex _idRegex = new Regex("\",\\s*\"t\":\\s*\"([^\"]+)");

        Match title = _titleRegex.Match(rawHtml);
        Match id = _idRegex.Match(rawHtml);
        Match quali = _qualiRegex.Match(rawHtml);

        string videotitle = title.Groups[1].Value;
        string videoid = youtubeurl.Substring(youtubeurl.IndexOf("?v=") + 3);
        string id2 = id.Groups[1].Value.Replace("%3D", "=");
        string dlurl = string.Format(getUrl, videoid, id2);

        fileext = "flv";
        if (rawHtml.Contains("'IS_HD_AVAILABLE': true")) // 1080p/720p
        {
          dlurl += "&fmt=" + quali.Groups[1].Value;
          fileext = "mp4";
        }
        else
        {
          dlurl += "&fmt=" + quali.Groups[1].Value;
          if (quali.Groups[1].Value == "18") // Medium
            fileext = "mp4";
          else if (quali.Groups[1].Value == "17") // Mobile
            fileext = "3gp";
        }
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        webClient.OpenReadAsync(new Uri(dlurl));
    }

    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        var file = IsolatedStorageFile.GetUserStoreForApplication();
        file.CreateDirectory("YoutubeDownloader");

        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file." + fileext, System.IO.FileMode.Create, file))
        {
            byte[] buffer = new byte[1024];
            while (e.Result.Read(buffer, 0, buffer.Length) > 0)
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

当您将数据从结果复制到独立存储时,无论您读了多少,您总是在写入缓冲区的全部内容。您的循环应该如下所示:

using (var stream = new IsolatedStorageFileStream("file." + fileext,
                                                  FileMode.Create, file))
{
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = e.Result.Read(buffer, 0, buffer.Length)) > 0)
    {
        stream.Write(buffer, 0, bytesRead);
    }
}
(我还没有检查这是否都是错的,但听起来好像有帮助…)


免责声明:我为谷歌工作,但我不知道你对YouTube许可证所做的任何事情的合法性。请不要将这个答案视为谷歌观点的任何暗示。就我而言,这个答案纯粹是对一些一般流处理的修复。

除了其他任何问题,您的“复制”方法是不正确的-您总是在写入
缓冲区。长度
字节,即使您读取的字节数小于此值……当您调用
读取
时,您应该记住结果,因此,您可以在调用中使用它来编写:
while((bytesRead=stream.Read(buffer,0,buffer.Length))>0)
…谢谢-您真的帮助了我。-但我不能把这个问题标记为已解决,因为你“只”写了一条评论,对吗?那就是所有的错误吗?好的,我会把它写下来作为一个答案…我没有错误代码-但我也没有windows phone,所以我不知道它是否真的有效。