Search code examples
iosvideo-streamingavplayeravplayerlayer

AVSynchronizedLayer animation works fine with local file but not while streaming


I have an animation that works fine with a local video file using an AVSynchronizedLayer. Now, when I replace the local file like with a streaming link(of the same file) the animation totally stops working. I have no clue whats causing this, have tried it multiple times and it works totally fine on local files but not while streaming. Any ideas?

EDIT:

- (void)observeValueForKeyPath:(NSString*) path
                  ofObject:(id)object
                    change:(NSDictionary*)change
                   context:(void*)context{
/* AVPlayerItem "status" property value observer. */
if (context == AVPlayerDemoPlaybackViewControllerStatusObservationContext)
{
    [self syncPlayPauseButtons];

    AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
    switch (status)
    {
            /* Indicates that the status of the player is not yet known because
             it has not tried to load new media resources for playback */
        case AVPlayerStatusUnknown:
        {
            [self removePlayerTimeObserver];
            [self syncScrubber];

            [self disableScrubber];
            [self disablePlayerButtons];
        }
            break;

        case AVPlayerStatusReadyToPlay:
        {
            /* Once the AVPlayerItem becomes ready to play, i.e.
             [playerItem status] == AVPlayerItemStatusReadyToPlay,
             its duration can be fetched from the item. */

            [self initScrubberTimer];

            [self enableScrubber];
            [self enablePlayerButtons];
        }
            break;

        case AVPlayerStatusFailed:
        {
            AVPlayerItem *playerItem = (AVPlayerItem *)object;
            [self assetFailedToPrepareForPlayback:playerItem.error];
        }
            break;
    }
}
/* AVPlayer "rate" property value observer. */
else if (context == AVPlayerDemoPlaybackViewControllerRateObservationContext)
{
    [self syncPlayPauseButtons];
}
/* AVPlayer "currentItem" property observer.
 Called when the AVPlayer replaceCurrentItemWithPlayerItem:
 replacement will/did occur. */
else if (context == AVPlayerDemoPlaybackViewControllerCurrentItemObservationContext)
{
    AVPlayerItem *newPlayerItem = [change objectForKey:NSKeyValueChangeNewKey];

    /* Is the new player item null? */
    if (newPlayerItem == (id)[NSNull null])
    {
        [self disablePlayerButtons];
        [self disableScrubber];
    }
    else /* Replacement of player currentItem has occurred */
    {
        /* Set the AVPlayer for which the player layer displays visual output. */
        [self.playbackView setPlayer:_player];

        /* Specifies that the player should preserve the video’s aspect ratio and
         fit the video within the layer’s bounds. */
        [self.playbackView setVideoFillMode:AVLayerVideoGravityResizeAspect];

        [self syncPlayPauseButtons];
    }
}
else if( context == AVPlayerDemoPlaybackViewControllerCurrentItemObservationContextForDisplay)
{
    AVPlayerLayer *layer = (AVPlayerLayer*) object;
    if (layer.readyForDisplay){
        [layer removeObserver:self forKeyPath:kReadyForAdDisplayKeyT];

        AVPlayerItem *item = _player.currentItem;
        AVSynchronizedLayer *syncedLayer = [AVSynchronizedLayer synchronizedLayerWithPlayerItem:item];
        syncedLayer.frame = CGRectMake(0, 0, 568, 320);
        syncedLayer.backgroundColor = [[UIColor clearColor] CGColor];
        //ADDING THE SYNCHRONIZED LAYER TO MY VIEW
        [self.madnessView addSyncedLayer:syncedLayer player:[self.playbackView player] withGravity:layer.videoGravity];
    }
}
else
{
    [super observeValueForKeyPath:path ofObject:object change:change context:context];
}}

This is where I'm adding the AVSynchronized layer to my custom view where an animation is played. The code for Animation is:

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
anim.beginTime = AVCoreAnimationBeginTimeAtZero;
anim.duration = self.videoDuration;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.values = [self.transforms objectAtIndex:adNumber];
UIImageView* imageView = [self.adViews objectAtIndex:adNumber];
[imageView.layer addAnimation:anim forKey:@"transformAnimation"];
imageView.hidden = NO;

Solution

  • The problem was with when the layer was created. Once i fixed that(i.e creating the layer/animation after the video player was ready to play), things started working as expected.