title: AVQueuePlayer starting next item too soon
body: <p>I've got an <code>AVQueuePlayer</code>, and under <em>specific</em> circumstances, it will start playing the next track before the previous one has finished.  It doesn't play them both simultaneously, it just cuts out the first track early and starts the next one.  This definitely happens when both tracks are http streams, I have<strike>n't</strike> tried when playing the files locally.  Also, this doesn't happen with every track.  Only a specific two tracks, when put together, will cause the issue.  Most tracks don't have this problem, but a significant number do.  Other media players do not show these symptoms when fed these tracks.  The tracks that show the symptoms <em>have</em> to be AAC encoded, I have no such issues when streaming their MP3 versions.  The AAC files are encoded using <code>libfaac</code>, VBR 90%.  The exact command line to encode the tracks:</p>
  
  <pre><code>ffmpeg -loglevel error -probesize 10000000 -i "$input" -strict -2 -acodec libfaac -q:a 90 -vn "$output.m4a"
  </code></pre>
  
  <p><strike>I believe the issue is <em>related</em> to <code>AVQueuePlayer</code> aggressively trying to play tracks gaplessly, as I know there's some metadata contained in AAC that allows two tracks to be played in sequence with no gap (particularly useful in electronic mixes) - these files should not have this metadata though, and surely it wouldn't cause <code>AVQueuePlayer</code> to freak out when playing two <em>unrelated</em> tracks?</strike> Edit: Determined not to be the cause, see below.</p>
  
  <p>If you want to reproduce the issue yourself, it takes an extremely minimal amount of code, just create a basic project, include <code>AVFoundation</code>, and do something like the below:</p>
  
  <pre><code>self.queuePlayer = [AVQueuePlayer queuePlayerWithItems: @[[AVPlayerItem playerItemWithURL: [NSURL URLWithString: @"https://eqbeats.org/track/4875/aac"]], [AVPlayerItem playerItemWithURL: [NSURL URLWithString: @"https://eqbeats.org/track/4499/aac"]]]];
  [self.queuePlayer play];
  </code></pre>
  
  <p>Let the first track play to almost-completion (it'll run for about 100 seconds or so), and you'll hear it abruptly end and the second track will come on.</p>
  
  <p>There are workarounds I think, such as intercepting a message when the next track starts and checking the previous track's play state, or adding the next item to the queue only after the previous starts... but I worry about the reliability of such methods, particularly when the app is backgrounded.  If there's no fix for this client side, I can investigate if there's a way to strip out this metadata server side (assuming that <em>is</em> the issue) or just resign myself to using the much larger, much worse-sounding MP3 versions.</p>
  
  <p><code>AVFoundation</code>'s just a riot - I'm sure anyone who's had the <em>horror</em> of working with it will agree.  I'm debating whether to raise this as a bug with Apple, but since it's survived <em>two</em> major iOS updates, I get this feeling this stuff is just business-as-usual for <code>AVQueuePlayer</code>.  Judging from my research on this issue, I'm clearly not the only one struggling with this framework:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/14247306/avqueueplayer-frustrations-buffer-underrun-handling">AVQueuePlayer frustrations (buffer underrun handling)</a></li>
  <li><a href="https://stackoverflow.com/questions/14115482/avplayer-streaming-stops-in-background-due-to-gaps">AVPlayer streaming stops in background due to gaps</a></li>
  <li><a href="https://stackoverflow.com/questions/4957216/avqueueplayer-sometimes-moves-to-the-next-avplayeritem-in-its-items-queue-withou">AVQueuePlayer sometimes moves to the next AVPlayerItem in its items queue without sending AVPlayerItemDidPlayToEndTimeNotification</a></li>
  <li><a href="https://stackoverflow.com/questions/8201471/avqueueplayer-playing-several-audio-tracks-in-background-ios5">AVQueuePlayer playing several audio tracks in background iOS5</a></li>
  <li><a href="https://stackoverflow.com/questions/15346127/detecting-current-item-in-avqueueplayer-and-knowing-when-it-changes">Detecting Current Item in AVQueuePlayer and Knowing when it Changes?</a></li>
  </ul>
  
  <p>Full disclosure: My app is actually written in RubyMotion, but I've been able to reproduce the issue in Objective-C with a lot less code (so a lot less possible points of failure), so I'm fairly certain RubyMotion or my use of <code>AVFoundation</code> is not the culprit here.</p>
  
  <p>Edit: Just tested it with local files using <code>file://</code> links, the problem is still there so it's definitely not a problem with the streaming engine or the webserver.</p>
  
  <p>Edit 2: I've looked into the MP4 spec and how iTunes and AVFoundation determine gaplessness in MP4.  Turns out in the <code>moov.udta.meta.ilst.---</code> atom, there should be three atoms, <code>mean</code>, <code>name</code>, and <code>data</code>, which tells the decoder <a href="http://sourceforge.net/p/mediainfo/feature-requests/398/" rel="nofollow noreferrer">various bits needed to recreate gapless</a>.  The files that cause the issue however <strong>do not have these atoms</strong> so it's definitely not a result of gapless playback.  I decided to run the original files (one's a FLAC, the other's an MP3) through CoreAudio's AAC encoder, and the resulting files play perfectly fine through <code>AVQueuePlayer</code> (even after I strip out all the gapless tags with <a href="http://atomicparsley.sourceforge.net/" rel="nofollow noreferrer"><code>AtomicParsley</code></a>), so it looks like a bug in <code>libfaac</code>'s output or <code>AVFoundation</code>'s decoder when dealing with non-Apple AAC bitstreams.</p>