title: Knowing when AVPlayer object is ready to play
body: <p>I'm trying to play an <code>MP3</code> file that is passed to an <code>UIView</code> from a previous <code>UIView</code> (stored in a <code>NSURL *fileURL</code> variable).</p>
  
  <p>I'm initializing an <code>AVPlayer</code> with:</p>
  
  <pre><code>player = [AVPlayer playerWithURL:fileURL];
  
  NSLog(@"Player created:%d",player.status);
  </code></pre>
  
  <p>The <code>NSLog</code> prints <code>Player created:0,</code> which i figured means it is not ready to play yet.</p>
  
  <p>When i click the play <code>UIButton</code>, the code i run is:</p>
  
  <pre><code>-(IBAction)playButtonClicked
  {
      NSLog(@"Clicked Play. MP3:%@",[fileURL absoluteString]);
  
      if(([player status] == AVPlayerStatusReadyToPlay) &amp;&amp; !isPlaying)
  //  if(!isPlaying)
      {
          [player play];
          NSLog(@"Playing:%@ with %d",[fileURL absoluteString], player.status);
          isPlaying = YES;
      }
      else if(isPlaying)
      {
  
          [player pause];
          NSLog(@"Pausing:%@",[fileURL absoluteString]);
          isPlaying = NO;
      }
      else {
          NSLog(@"Error in player??");
      }
  
  }
  </code></pre>
  
  <p>When i run this, I always get <code>Error in player??</code> in the console.
  If i however replace the <code>if</code> condition that checks if <code>AVPlayer</code> is ready to play, with a simple <code>if(!isPlaying)</code>..., then the music plays the SECOND TIME I click on the play <code>UIButton</code>.</p>
  
  <p>The console log is:</p>
  
  <pre><code>Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
  Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 0**
  
  Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
  Pausing:http://www.nimh.nih.gov/audio/neurogenesis.mp3
  
  Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
  2011-03-23 11:06:43.674 Podcasts[2050:207] Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 1**
  </code></pre>
  
  <p>I see that the SECOND TIME, the <code>player.status</code> seems to hold 1, which I'm guessing is <code>AVPlayerReadyToPlay</code>.</p>
  
  <p>What can I do to have the playing to work properly the first time i click the play <code>UIButton</code>?
  (ie, how can i make sure the <code>AVPlayer</code> is not just created, but also ready to play?)</p>