【问题标题】:How to restore full screen video player after PictureInPicture画中画后如何恢复全屏视频播放器
【发布时间】:2015-12-16 19:41:37
【问题描述】:

我正在使用 AVPlayerViewControllerallowsPictureInPicturePlayback = YES;

所以当我点击 PictureInPicture 按钮时,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

【问题讨论】:

    标签: ios objective-c iphone cocoa-touch ios9


    【解决方案1】:

    我能够通过在委托的playerViewController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler: 中使用presentViewController: animated: 来解决此问题

    所以它会在之前被解除后再次代表AVPlayerViewController

    我的新代码:

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

    【讨论】:

    • 来自文档:“completionHandler 要恢复用户界面,您必须实现您的方法以在此块中返回 YES 值。”
    • 你也可以在playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart上返回false。
    【解决方案2】:

    有没有办法用 WKWebView/UIWebView 做到这一点?画中画在那里免费提供,但没有可订阅的 AVPlayerViewController,也没有画中画委托事件。

    【讨论】:

    • 尝试将此作为一个新问题提出,以便您更快得到答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2010-12-07
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 2013-03-08
    • 2018-08-05
    相关资源
    最近更新 更多