我正在研究 AVFoundation,我想创建一个类似于 Whatsapp 的东西,例如当看到一个以 PiP 模式播放的 youtube 视频时。出于这个原因,我像这样创建视图控制器:
#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 按钮,但我不明白为什么。