2014-07-16 90 views
3

我有一個動畫,使用AVSynchronizedLayer與本地視頻文件正常工作。現在,當我使用流式鏈接(同一文件)替換本地文件時,動畫完全停止工作。我不知道是什麼導致了這種情況,已經嘗試過多次,並且它在本地文件上工作得很好,但是在流式傳輸時沒有問題。有任何想法嗎?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層到播放動畫時我的自定義視圖。對於動畫的代碼是:

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; 
+0

請向我們顯示您的代碼。 – Neeku

+0

已添加。請告訴我是否需要更多細節? –

回答

0

問題在於圖層創建時。一旦我解決了這個問題(即在視頻播放器準備播放後創建圖層/動畫),事情就會如預期般運作。

0

檢查self.videoDuration價值,我有同樣的問題,而我的流媒體視頻 - 持續時間始終爲0。希望這有助於。

+0

無用評論得到一些賞金..不讚賞。 –