0

I have an app that's 98% finished and very recently I stumbled upon a newly introduced bug. The app plays mp3's from a server coming from a playlist ( just a JSON list of track URL's, not an m3u file ).

Everything is working as expected, except when a track finishes playing. At that moment the next track is auto selected and the UI is updated. But the track is never played. Also: the playbackLikelyToKeepUp key value observer is not triggered. I can read the duration, … of that selected ( not playing ) AVPlayerItem. So it is a valid object.

Everything does work when I press "next" or select a row in my tableview. The strangest part for me is that the code is exactly the same for the user event ( pressing "next" ) and the auto play functionality. Both set a currentTrackIndex on my PlaylistManager, which I observe for changes. This triggers a playCurrentTrack method.

Is it possible it has something to do with NSNotificationCenter?
I use this to check if the current track has finished playing, like so:

[[NSNotificationCenter defaultCenter]   addObserver:self
                                        selector:@selector(playerItemDidReachEnd:)
                                        name:AVPlayerItemDidPlayToEndTimeNotification
                                        object:self.currentAudioPlayerItem];

playerItemDidReachEnd looks like this:

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    // same action as pressing 'next' button in my audioplayer UI controls
    // which is working, I can also see some UI updates ( correct title etc. … )
    [self pressedNext];
}

I'm thinking something with threads maybe, blocking the events that are triggered when loading the mp3 from the url? I see the mp3 information is being loaded in my http traffic tool.

Thanks.

Webdevotion
  • 1,223
  • 13
  • 24

1 Answers1

0

I've (hot)fixed this issue by putting some class methods back inline:

  • moved my shared audioplayer class inline
  • creating a new audioplayer for each newly selected AVPlayerItem
  • using a more bug-proof method of triggering the selector when player has reached end:

    -(void)playerItemDidReachEndNotification{
    // make sure we trigger the selector on the main thread
    [self performSelectorOnMainThread:@selector(playerItemDidReachEnd) withObject:nil waitUntilDone:NO];
    }

I'm sure some of this stuff is not necessary but the main goal was to get it working again.
Hope to find the real culprit in a next iteration.

Webdevotion
  • 1,223
  • 13
  • 24