问题描述
我们的应用程序是基于视频/音频的应用程序,并且我们已将所有媒体上传到Windows Azure.
Our app is video/audio based app, and we have uploaded all the media on Windows Azure.
但是,需要方便用户按需下载音频/视频文件,以便他们可以在本地播放.
However it is required to facilitate user to download audio/video file on demand, so that they can play it locally.
因此,我需要以编程方式下载音频/视频文件,并将其保存在IsolatedStorage中.
So i need to download audio/video file programmatically and save it in IsolatedStorage.
我们为每个音频/视频提供Windows Azure媒体文件访问URL.但是我陷入了下载媒体文件的第一步.
We have Windows Azure Media File Access URLs for each audio/video. But I am stuck in 1st step of downloading media file.
我用Google搜索并遇到了这篇文章,但是对于WebClient,没有可以使用的功能DownloadFileAsync.
I googled and came across this article, but for WebClient there is no function DownloadFileAsync that I can use.
但是,我尝试了其其他功能DownloadStringAsyn,并且下载的媒体文件为字符串格式,但不知道如何将其转换为音频(wma)/视频(mp4)格式.请提出建议,我该如何进行?还有其他下载媒体文件的方法吗?
However I tried its other function DownloadStringAsyn, and download media file is in string format but don't know how to convert it to audio(wma)/video(mp4) format. Please suggest me, how can I proceed? Is there other way to download media file?
这是我使用的示例代码
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();
}
推荐答案
将其保存在工具箱中:)
Save this in your toolbox :)
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;
}
这篇关于在Windows Phone 8中以编程方式下载媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!