我想用 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];
}];
}