2
votes

I am developing a music player in wp7 using silverlight.My end users don't have good internet connection when they are roaming. I want to give them an option to download music in their phone. For that i have written a download manager which they can use to download music when they have wifi connection. I ussue i am facing is that,My music is stored on my server and i am downloading the music on the phone using WebCLient class. if size of file goes beyond 68 MB , my application gets OutOfMemory exception. here is the code:

public void DownloadKirtan(KirtanViewModel kirtanVm,bool QueueItem=true) {

        {
            if (kirtanVm.LocationPath != null)
            {
                WebClient webClient = new WebClient();
                //webClient.AllowWriteStreamBuffering = false;
               // webClient.AllowReadStreamBuffering = false;
                if (QueueItem == false)
                {
                    //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload[kirtanVm] = webClient;
                   // App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Add(kirtanVm//
                    webClient = App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload[kirtanVm];
                    kirtanVm.IsDownloadedForOfflineViewing = "Started";
                    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                    webClient.OpenReadAsync(kirtanVm.LocationPath, kirtanVm);
                }
                else if (!App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.ContainsKey(kirtanVm))
                {
                    App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Add(kirtanVm, webClient);
                    kirtanVm.IsDownloadedForOfflineViewing = "Started";
                    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
                    webClient.OpenReadAsync(kirtanVm.LocationPath, kirtanVm);


                }
                // webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);

            }
        }
    }

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {

        KirtanViewModel kirtanVm = e.UserState as KirtanViewModel;
        try
        {
            if (e.Cancelled == false)
            {
                if (e.Result != null)
                {

                    ((WebClient)sender).OpenReadCompleted -= webClient_OpenReadCompleted;

                    #region Isolated Storage Copy Code


                    IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();



                    bool checkQuotaIncrease =  IncreaseIsolatedStorageSpace(e.Result.Length);


                    if (checkQuotaIncrease)
                    {

                        string VideoFile = "";
                        VideoFile = GetUrlOfOfflineContent(kirtanVm);

                        using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile))
                        {
                            long VideoFileLength = (long)e.Result.Length;


                            byte[] byteImage = new byte[VideoFileLength];

                            e.Result.Read(byteImage, 0, byteImage.Length);

                                isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);


                                        kirtanVm.IsDownloadedForOfflineViewing = "True";
                                        kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                        kirtanVm.DownloadProgress = "100%";
                                        Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                        AddRemoveKirtanInOfflineModels(kirtanVm, true);





                        }

                    #endregion
                    }
                    else
                    {
                        kirtanVm.IsDownloadedForOfflineViewing = "False";
                        App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
                        MessageBox.Show("There is not enough space in your phone to store this media. You need " + e.Result.Length + " bytes of storage.Please free some offline contents you have downloaded by going to Offline content managemnt screen in Settings Section and try again or increase the storage on your phone.");
                    }



                    // mediaFile.SetSource(isolatedStorageFileStream);

                    // mediaFile.Play();

                    // progressMedia.Visibility = Visibility.Collapsed;





                  }
            }

        }

        catch (Exception ex)
        {
            kirtanVm.IsDownloadedForOfflineViewing = "Failed";

            //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
           MessageBox.Show(ex.ToString());

        }

    }

The problem i am having is When i get e.result which is a stream object. To write to isolatedStoreFileStream, i have to read it again to byte array and then save it to isolatedstoragefile. This is inefficient, it consumes double memory. Memory of 60 MB (for 60MB file) by WebClient e.result stream object and then 60 MB to convert to array. So memory consumtion is 128 MB . Is there is better way to download big files and store it to IsolatedStorage>

UPDATE : I am now using following code using the chunck size , instead of reading all in memory but i still get out of memory error on large files 100 MB

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {

        KirtanViewModel kirtanVm = e.UserState as KirtanViewModel;
        try
        {
            if (e.Cancelled == false)
            {
                if (e.Result != null)
                {

                    ((WebClient)sender).OpenReadCompleted -= webClient_OpenReadCompleted;

                    #region Isolated Storage Copy Code
                    bool checkQuotaIncrease =  IncreaseIsolatedStorageSpace(e.Result.Length);
                    if (checkQuotaIncrease)
                    {

                        string VideoFile = "";
                        VideoFile = GetUrlOfOfflineContent(kirtanVm);
                        ThreadPool.QueueUserWorkItem( k =>{
                        using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile))
                            {
                                long VideoFileLength = (long)e.Result.Length;


                                using (BinaryWriter writer = new BinaryWriter(isolatedStorageFileStream))
                                {
                                    Stream resourceStream = e.Result;//streamResourceInfo.Stream;
                                    long length = resourceStream.Length;
                                    byte[] buffer = new byte[32];
                                    int readCount = 0;
                                    using (BinaryReader reader = new BinaryReader(resourceStream))
                                    {                    // read file in chunks in order to reduce memory consumption and increase performance                    
                                        while (readCount < length)
                                        {
                                            int actual = reader.Read(buffer, 0, buffer.Length);
                                            readCount += actual;
                                            writer.Write(buffer, 0, actual);
                                        }
                                    }
                                }
                                 kirtanVm.IsDownloadedForOfflineViewing = "True";
                                kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                kirtanVm.DownloadProgress = "100%";
                                Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                AddRemoveKirtanInOfflineModels(kirtanVm, true);
                            }
                        }
                       });

                                //byte[] byteImage = new byte[VideoFileLength];

                                //e.Result.Read(byteImage, 0, byteImage.Length);
                                // e.re
                                //ThreadPool.QueueUserWorkItem( k =>{
                                //     isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);
                                //isolatedStorageFileStream.Close();
                                //Application.Current.RootVisual.Dispatcher.BeginInvoke( ()=>
                                //    {
                                //        kirtanVm.IsDownloadedForOfflineViewing = "True";
                                //        kirtanVm.DownloadSize = "Size=" + (VideoFileLength / 1000000).ToString() + " MB";
                                //        kirtanVm.DownloadProgress = "100%";
                                //        Settings.OfflineKirtanContents.Value.Add(kirtanVm);
                                //        AddRemoveKirtanInOfflineModels(kirtanVm, true);
                                //    });

                                //});
                                //StreamWriter writer=new StreamWriter(isolatedStorageFileStream);
                                // writer.Write(e.Result);
                                // writer.Close();
                                // isolatedStorageFileStream.Write(
                                // e.Result.Write(

                              //  isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);
                                // isolatedStorageFileStream.Close();




                            }


                    #endregion

                    else
                    {
                        kirtanVm.IsDownloadedForOfflineViewing = "False";
                        App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
                        MessageBox.Show("There is not enough space in your phone to store this media. You need " + e.Result.Length + " bytes of storage.Please free some offline contents you have downloaded by going to Offline content managemnt screen in Settings Section and try again or increase the storage on your phone.");
                    }

                }
            }
            else
            {
                lock (App.ViewModel.LockForCancelForCurrentOfflineDownload)
                {
                    if (App.ViewModel.cancelInitiatedByUserForCurrentOfflineDownload)
                    {
                        kirtanVm.IsDownloadedForOfflineViewing = "False";
                       // kirtanVm.IsOfflineDownloadCancelled = false;
                        App.ViewModel.cancelInitiatedByUserForCurrentOfflineDownload = false;
                    }
                    else
                    {
                        if (kirtanVm.IsDownloadedForOfflineViewing == "Started")// don't queue things again
                        {
                            kirtanVm.IsDownloadedForOfflineViewing = "Failed";
                            // bool ItemExist = App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Any(k => k.Key.Kirtan.KirtanId == kirtanVm.Kirtan.KirtanId);

                            DownloadKirtan(kirtanVm, false);
                        }
                    }
                }
            }



        }

        catch (Exception ex)
        {
            kirtanVm.IsDownloadedForOfflineViewing = "Failed";

            //App.ViewModel.ActiveInstancesOfWebClientForKirtanDownload.Remove(kirtanVm);
           MessageBox.Show(ex.ToString());

        }

    }

Thanks Verinder

2

2 Answers

1
votes

Finally i found the solution

turn on webClient.AllowReadStreamBuffering = false; in the above code which i shared and then in void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {} method.. Remove all refrences to e.Result.Length. When using no buffering solution you cannot know the stream size in advance

long length = resourceStream.Length;                                     
byte[] buffer = new byte[1024];                                     
int readCount = 0;                                     
using (BinaryReader reader = new BinaryReader(resourceStream))                                     {                    // read file in chunks in order to reduce memory consumption and increase performance                                                             
while (true)                                         
{       
int actual = reader.Read(buffer, 0, 1024);   
 if(actual==0)
{
 break;
}
else
   {                                      
 readCount += actual;                                             
writer.Write(buffer, 0, actual);  
}                                       
}                                     
}

Keep on reading the stream with 1024 bytes at a time unless you are done reading. This way WebClient will not buffer whole file in memory and max mem usage on your phone will be 1KB at a time. You can download Gigs of data like this. INcrease the buffer size to 1MB or 3 MB , if you want to read faster. Make sure you run this code in ThreadPool instead of user thread

0
votes

You need to write it one chunk at a time by reading into a 4KB (or so) array and writing that to isolated storage until you run out of data.