I have done a lot of research, both on Google and StackOverflow. All the answers I found do not work in iOS 7. I started writing fresh app in iOS 7 SDK with Xcode 5.
All
Apparently the problem was on Apple's side as iOS update 7.0.3 fixes this issue. Besides what Alex noted about UIEventSubtype changes the code that worked on iOS6 now works on iOS7.
For sake of completeness, here is my relevant code that is working in both iOS6 and iOS7 - after the udpate to 7.0.3. Also included AVFoundation.framework and MediaPlayer.framework in project Build Phases -> Link binary with libraries. No code for this in app delegate.
In viewcontroller .h file:
#import
#import
@interface NewsDetailViewController : UIViewController
@property (nonatomic) MPMoviePlayerController *audioPlayer;
In viewcontroller .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
self.audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:audioUrl];
[self.audioPlayer prepareToPlay];
[self.audioPlayer.view setFrame:CGRectMake(0, 0, self.audioView.frame.size.width, 42)];
self.audioPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.audioView addSubview:self.audioPlayer.view];
[self.audioPlayer play];
NSError *setCategoryError = nil;
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setActive:YES error:&activationError];
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.audioPlayer stop];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.audioPlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[self.audioPlayer pause];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
if (self.audioPlayer.playbackState == MPMoviePlaybackStatePlaying) {
[self.audioPlayer pause];
}
else {
[self.audioPlayer play];
}
break;
default:
break;
}
}
}