Downloading Media File programmatically in Windows Phone 8

左心房为你撑大大i 提交于 2019-12-06 03:58:57

问题


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.

So i need to download audio/video file programmatically and save it in IsolatedStorage.

We have Windows Azure Media File Access URLs for each audio/video. But I am stuck in 1st step of downloading media file.

I googled and came across this article, but for WebClient there is no function DownloadFileAsync that I can use.

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?

Here is sample code that I used

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:


For downloading binary data, use [WebClient.OpenReadAsync][1]. You can use it to download data other than string data.

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

private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{

}



回答2:


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;
    }


来源:https://stackoverflow.com/questions/15880890/downloading-media-file-programmatically-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!