gpt4 book ai didi

ios - AVSynchronizedLayer 动画适用于本地文件但在流式传输时效果不佳

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:53 28 4
gpt4 key购买 nike

我有一个动画,使用 AVSynchronizedLayer 可以很好地处理本地视频文件。现在,当我用(同一文件的)流链接替换本地文件时,动画完全停止工作。我不知道是什么原因造成的,已经尝试了多次,它在本地文件上工作得很好,但在流式传输时却不行。有什么想法吗?

编辑:

- (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];
}}

这是我将 AVSynchronized 层添加到播放动画的自定义 View 的地方。动画代码是:

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;

最佳答案

检查 self.videoDuration 值,我在流式传输视频时遇到了同样的问题 - 持续时间始终为 0。希望这对您有所帮助。

关于ios - AVSynchronizedLayer 动画适用于本地文件但在流式传输时效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24779276/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com