我在UIViewController
的viewDidAppear
方法中創建了AVPlayerViewController
和附加的AVPlayer
。但是,當我按下「完成」按鈕時,我的自定義視圖控制器會自動解除。如何攔截在AVPlayerViewController中完成按鈕的敲擊?
我想攔截這個動作,以便使用我自己的放鬆Segue,但我不知道如何做到這一點。我找到MPMoviePlayerViewController
的示例,但不是AVPlayerViewController
。
我發現MPMoviePlayerViewController
的代碼如下:
- (void)playVideo:(NSString *)aVideoUrl {
// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Set the modal transition style of your choice
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentModalViewController:playerVC animated:YES];
// Start playback
[playerVC.moviePlayer prepareToPlay];
[playerVC.moviePlayer play];
}
- (void)movieFinishedCallback:(NSNotification *)aNotification {
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded) {
MPMoviePlayerController *moviePlayer = [aNotification object];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Dismiss the view controller
[self dismissModalViewControllerAnimated:YES];
}
}
我要求蘋果公司對這個問題,他們已經答覆如下:
感謝您的蘋果開發者技術支持聯繫(DTS )。我們的 工程師已審覈您的請求,並得出結論認爲 沒有支持的方式來實現 當前的運輸系統配置所需的功能。
當用戶按下完成按鈕時,我想繼續在小屏幕上播放視頻,所以我需要avplayer對象,我該怎麼辦? – 2015-08-19 12:23:37
當您按完成時,AVPlayerViewController將停止播放並釋放AVPlayer。對於你所需要的,用AVPlayer創建你自己的ViewController,當用戶按下完成時,調整視圖的大小。 https://developer.apple.com/library/ios/samplecode/AVPlayerDemo/Introduction/Intro.html有一個創建自己的AVPlayerViewController的例子 – dev 2015-08-19 13:18:47
如果我不想子類化AVPlayerViewController(如Apple所示:不要子類AVPlayerViewController覆蓋此類的方法不受支持,並導致未定義的行爲 此處:https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVPlayerViewController_Class/index.html)有沒有辦法獲得通知? – 2015-08-26 09:06:46