我目前正在使用iOS 4.3.5并尝试让我的MPMoviePlayerViewController
在输入背景后继续播放。
我实现了
中描述的所有内容和
我还将UIBackgroundMode设置为audio
。
从TabBarApplication中调用我的自定义MPMoviePlayerViewController类:
NSURL *streamUrl = [NSURL URLWithString:STREAM_URL];
self.playerViewController = [[CustomMoviePlayerViewController alloc] initWithContentURL:streamUrl];
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playerViewController.moviePlayer];
// Present
[self presentMoviePlayerViewControllerAnimated:self.playerViewController];
// Play the movie!
self.playerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.playerViewController.moviePlayer prepareToPlay];
[self.playerViewController.moviePlayer play];
我的CustomMovePlayerController
内部如下所示:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
[super remoteControlReceivedWithEvent:event];
NSLog(@"remoteControlReceived");
NSLog(@"%d", [[AVAudioSession sharedInstance] isActive]);
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.moviePlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[self.moviePlayer pause];
break;
default:
break;
}
}
}
我的MPMoviePlayerViewController的主要问题是,它没有响应remoteControlReceivedWithEvent
消息,为什么会这样?我是否将错误的东西归类?基于Tabbar的应用程序是否阻止我这样做?
最后但并非最不重要 - applicationDidFinishLaunchingWithOptions
包含以下内容:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
我无法弄清楚缺少什么......非常感谢所有帮助!
答案 0 :(得分:1)
我想我可以回答这两个问题:)
您尝试播放哪种文件格式?我的应用程序已经使用MPMoviePlayerViewController一段时间用于背景音频,但最近有报告称它不再在后台播放。
原来是AAC文件;他们是播客的章节和封面艺术每章... iOS 5增加了对它们的改进支持,但它必须使MPMoviePlayerViewController认为它正在播放视频。从我能找到的iOS 4.3开始关闭背景视频。
尝试使用普通的MP3文件,这对我来说仍然有用。我准备将AAC问题记录为Apple的错误。
锁定屏幕和通知栏遥控器按钮都会发送UIEventSubtypeRemoteControlTogglePlayPause
事件,而不是单独播放和暂停。所以我处理这样的事件:
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if (controller.playbackState == MPMusicPlaybackStatePlaying) {
[controller pause];
} else {
[controller play];
}
break;
//etc
}