如何在对象中添加叠加在avplayer视频上?

时间:2018-05-30 12:20:04

标签: ios objective-c iphone overlay avplayer

- (void)playMethod
{
    NSURL *url = [[NSURL alloc] initWithString:@"https://www.rmp-streaming.com/media///bbb-360p.mp4"];
    player = [AVPlayer playerWithURL:url];
    controller = [[AVPlayerViewController alloc] init];
    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

    //to rotate to the landscape on click of play button
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
    [UINavigationController attemptRotationToDeviceOrientation];

    //to open the view
    controller.view.frame = CGRectMake(0,0,667,375);
    controller.player = player;
    controller.showsPlaybackControls = YES;
    player.closedCaptionDisplayEnabled = NO;




    CALayer* overlayLayer = [CALayer layer];
    UIImage* overlayImage = nil;

    overlayImage = [UIImage imageNamed:@"apple.png"];
    //Create the parent layer
    CALayer *parentLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, 20, 20);
    CALayer *videoLayer = [CALayer layer];
    videoLayer.frame = CGRectMake(0, 0, 20, 20);

    [parentLayer addSublayer: videoLayer];
    [parentLayer addSublayer: overlayLayer];

    //Composes the composited video frame with a core animation layer
    AVVideoCompositionCoreAnimationTool *animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer: videoLayer inLayer: parentLayer];




    // to add the back button on player
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 addTarget:self
                action:@selector(backMethod)
      forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"Back" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     button1.frame = CGRectMake(50.0, CGRectGetMinY(controller.view.frame)+10, 160.0, 40.0);
    button1.tag = 1001;
    [self.view addSubview:button1];
    [controller.view  bringSubviewToFront:button1];
    [player pause];
    [player play];
}

我正在尝试在视频上添加叠加层,但是使用上面的代码没有任何反应。能否请您建议我该如何处理代码。我无法看到对视频产生任何影响,它与以前一样。

1 个答案:

答案 0 :(得分:0)

您需要使用AVPlayerViewController的 contentOverlayView 属性。请在我的答案中找到示例代码:AVPlayer UITapGestureRecognizer not working