title: WP - saving audio from url to IsoStorage and then playing by background agent
body: <p>I get stream for audio:</p>
  
  <pre><code>public static Task&lt;Stream&gt; DownloadFile(Uri url)
  {
      var tcs = new TaskCompletionSource&lt;Stream&gt;();
      var wc = new WebClient();
      wc.OpenReadCompleted += (s, e) =&gt;
      {
          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;
  }
  </code></pre>
  
  <p>I saved it to IsoStorage:</p>
  
  <pre><code>public static void SaveStreamToFile(Stream streamToRead, string fileName)
  {
      var file = IsolatedStorageFile.GetUserStoreForApplication();
      using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, file))
      {
          byte[] buffer = new byte[1024];
          while (streamToRead.Read(buffer, 0, buffer.Length) &gt; 0)
          {
              stream.Write(buffer, 0, buffer.Length);
          }
      }
  }
  </code></pre>
  
  <p>and I saved serialized information about playlist again to IsoStorage and in background audio player I deserialize it and get information about tracks. Everything seems fine. I get everything in my playList property in AudioPlayer.</p>
  
  <p>The problem is when I tried to play tracks. More accurately when I hit next track button. The problem is that next song not always starts playing. When I add breakpoint for example
  to method <code>OnPlayStateChanged(...)</code>:</p>
  
  <pre><code>        case PlayState.Stopped:
              break; &lt;-- breakpoint is here
  </code></pre>
  
  <p>And I didnd't do anything. I just hit continue and every tracks after that is playing. But without this not every track is playing (some tracks just not start playing, there is just silence). I know it's not related to that state (or I think). I think that the problem could be with some buffering. When I placed breakpoint somewhere then the phone could do some things in background and then when I click continue it's ready to playing. How can I fix? Thanks</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>SimpleTrack class</p>
  
  <pre><code>public class TrackSimple
  {
      public string Title { get; set; }
      public string Artist { get; set; }
      public string Album { get; set; }
      public string TrackLink { get; set; }
      public string CoverUri { get; set; }
  
  
      public TrackSimple()
      {
  
      }
  
      public TrackSimple(string title, string artist, string album, string trackLink, string coverUri)
      {
          Title = title;
          Artist = artist;
          Album = album;
          TrackLink = trackLink;
          CoverUri = coverUri;
      }
  }
  </code></pre>
  
  <p>PlayList class</p>
  
  <pre><code>public class PlayList
  {
      public List&lt;TrackSimple&gt; Tracks { get; set; }
  
      public PlayList()
      {
          Tracks = new List&lt;TrackSimple&gt;();
      }
  }
  </code></pre>