Saturday 17 August 2013

MPMusicPlayerController applicationMusicPlayer hoses iPodMusicPlayer

MPMusicPlayerController applicationMusicPlayer hoses iPodMusicPlayer

I have an application where I would like to use the iPodMusicPlayer to
resume the user's currently selected iPod selection (an audiobook). At
other times I need to play music from the iPod library without disturbing
the state (selection/position) of the iPod player (so the user can pick up
his audiobook outside of my app where he left off listenting in my app).
The obvious way to do this is to play (resume) the user's audiobook using
the iPodMusicPlayer, then when switching to other music use the
applicationMusicPlayer (after first pausing the iPodMusicPlayer). Do
exactly that in exactly that sequence works ok, BUT... any reference to
the applicationMusicPlayer screws up the state of the iPodMusicPlayer from
then on, i.e. I can no longer play or get the duration of the
iPodMusicPlayer. Messages to the iPodMusicPlayer seem to be routed to the
applicationMusicPlayer.
I've adapted a simple example from another developer who had a similar
problem but worked around it by only using the applicationMusicPlayer. In
this test, the reference to the applicationMusicPlayer causes the
iPodMusicPlayer to misbehave. Commenting that line out causes correct
behavior.
Is sequential use of the two music players not supported? If so, that's
not documented anywhere that I can find.
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (strong, nonatomic) MPMusicPlayerController *MPPlayer;
- (void)updateProgressView;
@end
@implementation ViewController
@synthesize playPauseButton = _playPauseButton;
@synthesize progressView = _progressView;
@synthesize label = _label;
- (void)viewDidLoad {
[super viewDidLoad];
[[MPMusicPlayerController iPodMusicPlayer]
beginGeneratingPlaybackNotifications];
NSNotificationCenter *notificationCenter = [NSNotificationCenter
defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handle_NowPlayingItemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:[MPMusicPlayerController
iPodMusicPlayer]];
[notificationCenter addObserver:self
selector:@selector(handle_PlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:[MPMusicPlayerController
iPodMusicPlayer]];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(updateProgressView) userInfo:nil repeats:YES];
[self updateProgressView];
}
- (IBAction)playPauseButtonPressed {
// ****** this line (instantiation of the applicationMusicPlayer)
causes the iPodMusicPlayer to misbehave ******
NSLog(@"app player state:%i", [MPMusicPlayerController
applicationMusicPlayer].playbackState);
// ****** commented out the example behaves corectly
NSLog(@"iPod player state:%i", [MPMusicPlayerController
iPodMusicPlayer].playbackState);
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] ==
MPMusicPlaybackStatePlaying) {
NSLog(@"iPod pause");
[[MPMusicPlayerController iPodMusicPlayer] pause];
} else {
NSLog(@"iPod play");
[[MPMusicPlayerController iPodMusicPlayer] play];
}
}
- (void)handle_NowPlayingItemChanged:(NSNotification *)notification {
NSLog(@"now playing: %@", [[MPMusicPlayerController
iPodMusicPlayer].nowPlayingItem
valueForProperty:MPMediaItemPropertyTitle]);
self.label.text = [[MPMusicPlayerController
iPodMusicPlayer].nowPlayingItem
valueForProperty:MPMediaItemPropertyTitle];
}
- (void)handle_PlaybackStateChanged:(NSNotification *)notification {
NSLog(@"playback state: %i", [MPMusicPlayerController
iPodMusicPlayer].playbackState);
self.playPauseButton.selected = ([[MPMusicPlayerController
iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying);
}
- (void)updateProgressView {
NSTimeInterval currentTime = [[MPMusicPlayerController
iPodMusicPlayer] currentPlaybackTime];
NSNumber *duration = [[MPMusicPlayerController
iPodMusicPlayer].nowPlayingItem
valueForProperty:MPMediaItemPropertyPlaybackDuration];
self.progressView.progress = currentTime / [duration floatValue];
}
@end

No comments:

Post a Comment