MPMoviePlayerController播放背景

时间:2012-09-13 17:29:39

标签: iphone objective-c ios mpmovieplayercontroller multitasking

我想用MPMoviePlayerController在后台播放视频的声音。过去视频正在运行,但背景声音停止。我想继续声音和背景(仅在背景音中)。 info.plist将后台模式设置为“音频”

    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];


    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}




- (void)Play_Video{
    [[AVAudioSession sharedInstance] setDelegate: self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];


    self.movieController = [[MPMoviePlayerController alloc] init];

    self.movieController.view.frame = self.view.bounds;
    self.movieController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    self.movieController.backgroundView.backgroundColor = [UIColor clearColor];
    self.movieController.view.hidden = NO;
    self.movieController.controlStyle = MPMovieControlStyleEmbedded;
    [self.view addSubview:self.movieController.view];
    self.movieController.useApplicationAudioSession = NO;

    [UrlBlock VideoURL:url success:^(NSURL *URL) {

        if (self.movieController)
            self.movieController.contentURL = URL;
        else
            self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:URL];

    } failure:^(NSError *error) {
        [self performSelectorOnMainThread:@selector(Alert:) withObject:error waitUntilDone:NO];
    }];


}

1 个答案:

答案 0 :(得分:0)

这可能很明显,也许你只是没有粘贴它,但是你是否覆盖了- (BOOL) canBecomeFirstResponder方法?

- (BOOL) canBecomeFirstResponder {
    return YES;
}