Background Audio Part II: Copying Audio Files To Isolated Storage

Windows Phone Tutorial

In a previous article I showed how to use the audio background task agent to run the audio even when your application is no longer in the foreground. In that article we used audio that was downloaded from the Internet. If you wish to use local audio you must copy it to isolated storage.

While this may seem intimidating at first, the actual steps are straightforward and the process is relatively simple.

 

For example, assuming that you want to copy the three Sample audio files that come with Windows 7, you would create a method, CopyToIsolatedStorage, in your app.xaml.CS file.  The method GetUserStoreForApplication will return an IsolatedStorageFile which you will place in a using statement to ensure that it is closed as soon as you are finished with it, thus avoiding a resource leak

private void CopyToIsolatedStorage()
{
    using (IsolatedStorageFile storage = 
         IsolatedStorageFile.GetUserStoreForApplication())
    {

    }
}

Create an array of your file names, and then iterate through that array to add each to isolated storage.

 private void CopyToIsolatedStorage()
 {
     using (IsolatedStorageFile storage = 
         IsolatedStorageFile.GetUserStoreForApplication())
     {
         string[ ] files = new string[ ] 
         { 
             "Kalimba.mp3", 
             "Maid with the Flaxen Hair.mp3", 
             "Sleep Away.mp3" 
         };

         foreach (var _fileName in files)
         {

         }
     }
 }

 

Examine each filename to see if it is already in isolated storage.  If not, create the path and a StreamResourceInfo object based on the full file name and path. 

 if (!storage.FileExists( _fileName ))
 {
     string _filePath = "Audio/" + _fileName;
     StreamResourceInfo resource = 
         Application.GetResourceStream( 
         new Uri( _filePath, UriKind.Relative ) );

 

Next, create an IsolatedStorageFileStream (which again you’ll put into a using statement to prevent a resource leak) and read from the original file to the file in isolated storage, as bytes.

using (IsolatedStorageFileStream file = 
    storage.CreateFile( _fileName ))
{
    int chunkSize = 4096;
    byte[ ] bytes = new byte[chunkSize];
    int byteCount;

    while ((byteCount = 
        resource.Stream.Read( bytes, 0, chunkSize )) > 0)
    {
        file.Write( bytes, 0, byteCount );
    }
}

 

Here’s the complete method for context:

private void CopyToIsolatedStorage()
{
    using (IsolatedStorageFile storage = 
        IsolatedStorageFile.GetUserStoreForApplication())
    {
        string[ ] files = new string[ ] 
        { 
            "Kalimba.mp3", 
            "Maid with the Flaxen Hair.mp3", 
            "Sleep Away.mp3" 
        };

        foreach (var _fileName in files)
        {
            if (!storage.FileExists( _fileName ))
            {
                string _filePath = "Audio/" + _fileName;
                StreamResourceInfo resource = 
                    Application.GetResourceStream( 
                    new Uri( _filePath, UriKind.Relative ) );

                using (IsolatedStorageFileStream file = 
                    storage.CreateFile( _fileName ))
                {
                    int chunkSize = 4096;
                    byte[ ] bytes = new byte[chunkSize];
                    int byteCount;

                    while ((byteCount = 
                        resource.Stream.Read(
                        bytes, 0, chunkSize )) > 0)
                    {
                        file.Write( bytes, 0, byteCount );
                    }
                }
            }
        }
    }
}

 

Don’t forget to put a call to this method in the constructor:

 

public App()
{
    UnhandledException += Application_UnhandledException;
    CopyToIsolatedStorage();

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Background Agents, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

4 Responses to Background Audio Part II: Copying Audio Files To Isolated Storage

Comments are closed.