Resources not releasing with UIView

148 Views Asked by At

Here is how my two views are laid out:

enter image description here

Essentially my goal is to have VC2 start 'fresh' every time, with everything new, including a MPMusicPlayerController. I was under the impression that an unwindsegue removed the instance of the new view as well as it's resources but the music player is not being stopped. Before VC2 ends, it runs a function that deregisters all the media player notifications and sets the music player itself = nil. However I can still hear the music playing when it returns back to VC1 and when I create a new instance of VC2 my 'new' music player controller doesn't work (no notifications are registered).

Some code:

Music player is instantiated (VC2):

- (void)viewDidLoad
{
    [super viewDidLoad];
    musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
    [self registerMediaPlayerNotifications];
}

Register notifications:

- (void) registerMediaPlayerNotifications
{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver: self
                           selector: @selector (handle_NowPlayingItemChanged:)
                               name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                             object: musicPlayer];

    [notificationCenter addObserver: self
                           selector: @selector (handle_PlaybackStateChanged:)
                               name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                             object: musicPlayer];

    [notificationCenter addObserver: self
                           selector: @selector (handle_VolumeChanged:)
                               name: MPMusicPlayerControllerVolumeDidChangeNotification
                             object: musicPlayer];

    [musicPlayer beginGeneratingPlaybackNotifications];
}

Snippet from getNextSong

   MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:test forProperty:MPMediaEntityPropertyPersistentID];
    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    [query addFilterPredicate: predicate];
    NSArray *queryResults = [query items];
    for (MPMediaItem *song in queryResults) {
        [musicPlayer setQueueWithQuery:query];
        [musicPlayer play];
    }

Code that is executed before VC2 is 'unwound':

- (IBAction)reset:(id)sender {
    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                    name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                                  object: musicPlayer];

    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                    name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                                  object: musicPlayer];

    [[NSNotificationCenter defaultCenter] removeObserver: self
                                                    name: MPMusicPlayerControllerVolumeDidChangeNotification
                                                  object: musicPlayer];

    [musicPlayer endGeneratingPlaybackNotifications];
    [musicPlayer stops];
    musicPlayer = nil;

    [self performSegueWithIdentifier:@"resetRoom" sender:self];
}

Even though im setting it to nil, the music continues to play, which tells me its not actually being destroyed.

EDIT: To be more specific, the second time I instantiate a new copy of VC2 the following if statement is not executing:

- (void) handle_PlaybackStateChanged: (id) notification
{
    MPMusicPlaybackState playbackState = [musicPlayer playbackState];

    if(playbackState == MPMusicPlaybackStateStopped)
        [self getNextSong];

      [self playbackButton];
}

I have verified with the debugger that the playbackState is 0 both times I instantiate. However the second time, the if statement never happens.

0

There are 0 best solutions below