10

I am using a music player property for iPod player controller.

// .h
@property (nonatomic, retain) MPMusicPlayerController *ipodPlayer;

// .m
ipodPlayer = [MPMusicPlayerController iPodMusicPlayer];

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter addObserver:self selector:@selector(changedPlaybackState:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:ipodPlayer];
[notificationCenter addObserver:self selector:@selector(changedNowPlayingItem:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:ipodPlayer];

[ipodPlayer beginGeneratingPlaybackNotifications];

During background processing, if iPod player app is terminated, the console prints out:

MediaPlayer: Message playbackState timed out.

If it doesn't crash(or freezes, slowing performance), the notification is not being sent to my observing methods anymore. I can still send messages like:

[ipodPlayer pause];
[ipodPlayer play];
[ipodPlayer skipToNextItem];
[ipodPlayer skipToPreviousItem];

but can't receive any notifications

My questions are:

  1. Is there are way to reassign, reload pointers during runtime? How can I restore the property to be just like when it's first launched?
  2. How can I catch the messge:"MediaPlayer: Message playbackState timed out." in console output? This is not like using NSLog.

Thank you for helping me.

UPDATED: It seems like using assign or weak for ipodPlayer property was the solution. Also, accessing it is done with assumption that the property may not be there. After many trials and a year of actually using it in my app, I think this was the right solution.

petershine
  • 3,190
  • 1
  • 25
  • 49
  • Are you using iOS 5? If not, then you should be doing: self.ipodPlayer = [MPMusicPlayerController ...] Otherwise in theory the instance created by iPodMusicPlayer would be collected and you haven't upped the ref count so you would get a null pointer at some point. That's unless you call that method before every interaction with the music player, which may be another reason for your problems. One instance gets collected and you don't attach yourself to the new instance? – hktegner Dec 12 '11 at 08:54
  • Thank you for commenting this old post. I had changed my flow of accessing this ipodPlayer property with the possibility of its absence. So, I made the property to use `assign` or `weak` and initialized it with `[MPMusicPlayerController iPodMusicPlayer];` whenever it's not there – petershine Dec 13 '11 at 01:49

1 Answers1

1

I had similar issue with my MpMoviePlayerController in iOS 5. I Found a fix that took care of it. It might work here as well.

Add:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

in viewDidLoad.

More my other post

Community
  • 1
  • 1
rydgaze
  • 1,050
  • 13
  • 24
  • Thank you for answering this old post. Since I'm not using `AVAudioPlayer`, I don't know if this is the right solution for me. Instead, I made it sure to use weak assign for iPodPlayer property and ever since using it like this, the occurrence of crash doesn't happen almost to none. Guess it has to do with referencing an object which is not owned by me. Thanks anyway – petershine Nov 21 '11 at 01:18