【问题标题】:PiP AVPlayerViewController issue iPhonePiP AVPlayerViewController 问题 iPhone
【发布时间】:2019-01-08 19:35:58
【问题描述】:

我正在学习 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 设备上运行应用程序,我看不到画中画按钮,但我不明白为什么。

【问题讨论】:

  • 没人能帮我吗?
  • 我认为这是隐藏主题,如 CarPlay 或 NFC……没有一个结果。只需自定义无法在 ios 14 上使用的库

标签: ios avfoundation avplayerviewcontroller picture-in-picture


【解决方案1】:

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

【讨论】:

  • PiPhone - 这是你自己的库,用 Swift 编写。但他问及Objective-C
【解决方案2】:
  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)setupSupport { 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!");
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2020-07-25
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多