2012-02-09 5 views
1

Windows Phone에서 사용하기 위해 C#으로 작성된 Youtube Downloader 클래스를 수정했습니다. 내 결과는 다음과 같다. 그러나 내가 dw()를 부를 때; 웹 브라우저에서 e.Result.Read (buffer, 0, buffer.Length) 줄의 File-not-found-exception을 얻습니다. 나는 이것이 무엇을 의미하는지 확실히 알지만 그것을 고치는 법을 모른다. 따라서 두 가지 질문이 있습니다. 왜 코드가 작동하지 않습니까? 또는 WindowsPhone 7에서 Youtube 비디오를 다운로드 할 수있는 또 다른 방법이 있습니까? (도서관이나 무료 코드 조각처럼 ...) 고마워.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); 
      } 
     } 
    } 
} 
+1

"복사"방법이 잘못되었습니다. 그보다 작은 글자를 읽었을지라도 항상 * buffer.Length 바이트를 쓰고 있습니다. –

+0

대신 무엇을 사용할 수 있습니까? – user1200226

+0

'Read'를 호출하면, 그 결과를 기억해야합니다. 그래서 호출에서 그것을 사용할 수 있습니다 : while ((bytesRead = stream.Read (buffer, 0, buffer.Length)> 0)'. .. –

답변

3

결과에서 격리 된 저장소로 데이터를 복사하면 읽은 것보다 버퍼의 전체 내용을 항상 작성합니다. 귀하의 루프는 다음과 같아야합니다

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 라이선스와 관련하여 귀하가하고있는 행위의 적법성에 대해서는 잘 모릅니다. 이 답변을 Google의 의견에 대한 어떤 종류의 표시로 취급하지 마십시오. 이 대답은 내가 아는 한 순전히 일반적인 스트림 처리에 대한 수정입니다.

+0

401k에 계속 투자하십시오. – paislee

관련 문제