MPMoviePlayerViewController在后台运行并使用远程控制

时间:2011-08-01 00:46:04

标签: iphone ios ios4

我目前正在使用iOS 4.3.5并尝试让我的MPMoviePlayerViewController在输入背景后继续播放。

我实现了

中描述的所有内容

http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html

我还将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];

我无法弄清楚缺少什么......非常感谢所有帮助!

1 个答案:

答案 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
}