2

我正在研究 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 按钮,但我不明白为什么。

4

2 回答 2

-1

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

于 2019-04-15T15:21:27.607 回答
-1
  1. 对于一般的第一个 - 功能选择背景音频、AirPlay 和 PiP

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

    NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]; NSURL *url = [NSURL fileURLWithPath:path]; self.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:AVLayerVideoGravityResize]; [self.view.layer addSublayer:_layer];

    [_AVPlayer 播放];

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

    -(void)setupSuport { if([AVPictureInPictureController isPictureInPictureSupported]) {

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

    }

  4. 画中画的动作

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

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

}

于 2020-10-21T13:38:55.567 回答