如何在画中画后恢复全屏视频播放器

4
我正在使用allowsPictureInPicturePlayback = YES;AVPlayerViewController,当我点击画中画按钮时,AVPlayerViewController将被关闭并显示小型播放器。但是,当我在小型播放器中点击恢复按钮时,它会关闭而不显示AVPlayerViewController。那么,在单击小型播放器中的恢复按钮后,如何恢复AVPlayerViewController呢?以下是我的代码:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface ViewController ()<AVPlayerViewControllerDelegate>{
    AVPlayerViewController *_AVPlayerViewController;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    // create a movie player
    NSURL *url = [NSURL URLWithString:@"http://38.96.175.119:1935/aletejahtv/aletejahtv3/playlist.m3u8"];

    _AVPlayerViewController = [AVPlayerViewController new];
    _AVPlayerViewController.delegate = self;
    _AVPlayerViewController.showsPlaybackControls = YES;
    _AVPlayerViewController.allowsPictureInPicturePlayback = YES;
    _AVPlayerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
    _AVPlayerViewController.player = [AVPlayer playerWithURL:url];
    [_AVPlayerViewController.player play];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self presentViewController:_AVPlayerViewController animated:YES completion:nil];
    });
}


- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController{
    NSLog(@"playerViewControllerWillStartPictureInPicture");
}

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController{
    NSLog(@"playerViewControllerDidStartPictureInPicture");
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error{
    NSLog(@"failedToStartPictureInPictureWithError");
}

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController{
    NSLog(@"playerViewControllerWillStopPictureInPicture");
}

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController{
    NSLog(@"playerViewControllerDidStopPictureInPicture");
}

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController{
    return YES;
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler{
    NSLog(@"restoreUserInterfaceForPictureInPictureStopWithCompletionHandler");
}

@end
2个回答

3

我能够通过在代理中使用presentViewController: animated:方法来解决这个问题。

这样可以在之前被关闭的AVPlayerViewController再次显示。

我的新代码:

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler{
    [self presentViewController:playerViewController animated:YES completion:nil];
    NSLog(@"restoreUserInterfaceForPictureInPictureStopWithCompletionHandler");
}

4
从文档中得知:"completionHandler 为了恢复用户界面,您必须在此块中实现方法并返回YES的值。" - Leon
1
你也可以在 playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart 上返回 false。 - ricardopereira

0

有没有办法在WKWebView/UIWebView中实现这个功能?虽然它们自带画中画功能,但是却没有AVPlayerViewController可供订阅,也没有画中画委托事件。


尝试将此作为新问题提出,这样您可能会更快地得到答案。 - MujtabaFR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接