Download media programmatically in Windows Phone 8

Our application is a video / audio based application, and we have loaded all media in Windows Azure.

However, it is required that the user can download the audio / video file on demand, so that they can play it locally.

Therefore, I need to download the audio / video file programmatically and save it to IsolStorage.

We have Windows Azure file access URLs for each audio / video. But I got stuck at the first stage of downloading the media file.

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

However, I tried another DownloadStringAsyn function, and download the media file in a lowercase format, but I don’t know how to convert it to audio (wma) / video (mp4) format. Please suggest me how can I continue? Is there any other way to download a media file?

Here is an example of the code 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();
    }
+5
source share
2 answers

Use to download binary data [WebClient.OpenReadAsync][1]. You can use it to load 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)
{

}
+1
source

Save this in the toolbar :)

    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;
    }
+1
source

All Articles