PiP AVPlayerViewController issue iPhone



我正在学习AVFoundation,当我看到一个以PiP模式播放的youtube视频时,我想创建一个类似于Whatsapp的东西。出于这个原因,我创建了这样的视图控制器:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface ViewController () <AVPictureInPictureControllerDelegate>
@property (strong, nonatomic) AVPlayerViewController *playerViewController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url = [[NSURL alloc] initWithString:@"https://www.rmp-streaming.com/media/bbb-360p.mp4"];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];
AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
self.playerViewController = [[AVPlayerViewController alloc] init];
self.playerViewController.player = playVideo;
self.playerViewController.player.volume = 0;
self.playerViewController.view.frame = self.view.bounds;
[self.view addSubview:self.playerViewController.view];
self.playerViewController.showsPlaybackControls = YES;
self.playerViewController.allowsPictureInPicturePlayback = YES;
self.playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
[playVideo play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

在AppDelegate中,我这样做了:

NSError *errore = nil;
AVAudioSession *sessioneAudio = [AVAudioSession sharedInstance];
[sessioneAudio setActive:YES error:&errore];
[sessioneAudio setCategory:AVAudioSessionCategoryPlayback error:&errore];

我在项目的功能中启用了音频、AirPlay和画中画背景模式。

我的问题是:

如果我在iPad设备上运行应用程序,我会看到PiP按钮,我可以启用PiP模式。若我在iPhone设备上运行应用程序,我看不到PiP按钮,但我不明白为什么。

我已经实现了在iPhone上启用PiP模式的PiPhone框架。我认为它可以解决你的问题。

  1. 对于常规功能中的第一项,请选择背景音频、AirPlay和PiP

  2. 将我的代码粘贴到Objective-C 上

    NSString*path=[[NSBundle mainBundle]pathForResource:@";视频";类型:@";mp4"];NSURL*url=[NSURL文件URLWithPath:path];自己AVPlayer=[AVPlayer playerWithURL:url];self.layer=[AVPlayerLayer playerLayerWithPlayer:_AVPlayer];

    [self.layer setFrame:CGRectMake(0,0,self.view.frame.size.width-100,self.view.frame.size.height-72(];[self.layer setVideoGravity:AVLayerVideoGravity调整大小];[self.view.layer addSublayer:_layer];

    [_AVPlayer play];

  3. 检查Pip是否支持当前设备

    -(无效(setupSuport{if([AVPictureInPictureController is PictureInPictureSupported]({

    _AVPictureInPictureController =  [[AVPictureInPictureController alloc] initWithPlayerLayer:_layer];
    _AVPictureInPictureController.delegate = self;
    }
    else
    {
    // not supported PIP start button desable here
    }
    

    }

  4. PiP 的作用

-(IBAction(actionPIPStart:(UIButton*(sender{

if (_AVPictureInPictureController.pictureInPictureActive) {
[_AVPictureInPictureController stopPictureInPicture];
NSLog(@"no PIP!");
}
else {
[_AVPictureInPictureController startPictureInPicture];
NSLog(@"PIP!");
}

}

最新更新