2013-04-08 125 views
5

我們的應用程序是基於視頻/音頻的應用程序,我們已經上傳了Windows Azure上的所有媒體。在Windows Phone 8中以編程方式下載媒體文件

然而,它需要方便用戶按需下載音頻/視頻文件,以便他們可以在本地播放。

所以我需要以編程方式下載音頻/視頻文件並將其保存在IsolatedStorage中。

我們爲每個音頻/視頻提供Windows Azure媒體文件訪問URL。但我卡在下載媒體文件的第一步。

我使用Google搜索並遇到了this article,但對於WebClient,我沒有可以使用的功能DownloadFileAsync。

但是我嘗試了其他功能DownloadStringAsyn,下載媒體文件是字符串格式,但不知道如何將其轉換爲音頻(WMA)/視頻(MP4)格式。請建議我,我該怎麼辦?有其他方法可以下載媒體文件嗎?

下面是示例代碼,我使用

private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e) 
    { 
     WebClient mediaWC = new WebClient(); 
     mediaWC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(mediaWC_DownloadProgressChanged); 
     mediaWC.DownloadStringAsync(new Uri(link));    
     mediaWC.DownloadStringCompleted += new DownloadStringCompletedEventHandler(mediaWC_DownloadCompleted);   

    } 

    private void mediaWC_DownloadCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Cancelled) 
      MessageBox.Show("Downloading is cancelled"); 
     else 
     { 
      MessageBox.Show("Downloaded"); 
     } 
    } 

    private void mediaWC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
    { 
     statusbar.Text = status= e.ProgressPercentage.ToString(); 
    } 

回答

1

對於下載的二進制數據,使用[WebClient.OpenReadAsync][1]。您可以使用它來下載字符串數據以外的數據。

var webClient = new WebClient(); 
webClient.OpenReadCompleted += OnOpenReadCompleted; 
webClient.OpenReadAsync(new Uri("...", UriKind.Absolute)); 

private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 

} 
1

把它保存在你的工具箱:)

public static Task<Stream> DownloadFile(Uri url) 
    { 
     var tcs = new TaskCompletionSource<Stream>(); 
     var wc = new WebClient(); 
     wc.OpenReadCompleted += (s, e) => 
     { 
      if (e.Error != null) tcs.TrySetException(e.Error); 
      else if (e.Cancelled) tcs.TrySetCanceled(); 
      else tcs.TrySetResult(e.Result); 
     }; 
     wc.OpenReadAsync(url); 
     return tcs.Task; 
    } 
+0

u能提供這方面的一個完整的例子嗎?在調用DownloadFile後,我如何將文件保存到isolationStorage – 2014-02-05 08:34:58

相關問題