2016-06-01 1 views
0

:AVAsset의 "isPlayable"/ "재생"체크 블록 및 지연 UI 업데이트 다음 코드 세그먼트가 실행됩니다 사용자가 다음 UI 요소를 탭으로 재생 비디오를 호출 할 수 있습니다 어떤 시점에서 내 응용 프로그램에서

self.loadingView.frame = _frameWhereItShouldBeLocated; 
[self.loadSpinner startAnimating]; // self.loadSpinner is an UIActivityIndicatorView 
self.loadingView.hidden = FALSE; 

AVPlayer *player = [AVPlayer playerWithURL:fileUrl]; // fileUrl is where is the video file is hosted, which is not a local path 

if ([player.currentItem.asset isPlayable]) 
{ 

    if (!self.playerController) { 
     self.playerController = [[AVPlayerViewController alloc] init]; 
     self.playerController.transitioningDelegate = self; 
     self.playerController.modalPresentationStyle = UIModalPresentationCustom; 
    } 

    self.playerController.player = player; 
    self.playerController.showsPlaybackControls = TRUE; 

    [self.navigationController presentViewController:self.playerController animated:YES completion:nil]; 
    [self.playerController.player play]; 

} 

로딩 뷰는 사용자가 탭한 직후에 표시되고 잠시 후 플레이어 컨트롤러가 표시되어 비디오를 재생합니다. 그러나로드 뷰가 표시되기 전에 상당한 지연이 발생합니다.

User tap -> loading view shown -> (some time for loading the video, etc) -> play video 

대신이 내가있어 무엇 :

내가 기대했던 것입니다

User tap -> (significant time delay) -> loading view shown -> play video 

나는 지연이 즉, [player.currentItem.asset isPlayable] 호출에 의해 발생되는 것을 발견 디버깅 후 불러 오기 뷰는 호출이 반환 된 후에 만 ​​표시됩니다. 나는 dispatch_async 콜에서 뷰를로드하는 디스플레이 아래에 세그먼트를 놓으려고했으나 아무런 변화도 없었습니다.

예상대로 동작하도록하기 위해이 문제를 처리해야합니까?

고맙습니다. 관찰 된 속도의 새로운 값이 0 인 경우 if ([player.currentItem.asset isPlayable])

검사의

+0

대답은 AVAsynchronousKeyValueLoading의 도입에 있습니다 https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAsynchronousKeyValueLoading_Protocol/index.html –

답변

0

Insted이

if ((player.rate != 0) && (player.error == nil)) { 
    // player is playing 
} 

또는 당신은

[player addObserver:self 
       forKeyPath:@"rate" 
       options:NSKeyValueObservingOptionNew 
       context:NULL]; 

아래와 같이 rate 속성에 대한 통지를 추가 할 수 있습니다 그런 다음 확인 즉, 끝까지 도달하거나 빈 버퍼로 인해 스톨하는 것과 같은 이유로 재생이 중지되었습니다. 속도 == 0.0의 경우, 다음과 같은 검사를 할 수있는, 정확히 중지 재생 원인을 알 수있는

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary<NSString *,id> *)change 
         context:(void *)context { 
    if ([keyPath isEqualToString:@"rate"]) { 
     float rate = [change[NSKeyValueChangeNewKey] floatValue]; 
     if (rate == 0.0) { 
      // Playback stopped 
     } else if (rate == 1.0) { 
      // Normal playback 
     } else if (rate == -1.0) { 
      // Reverse playback 
     } 
    } 
} 

은 : 위의 조건에 따라

if (self.player.error != nil) { 
    // Playback failed 
} 
if (CMTimeGetSeconds(self.player.currentTime) >= 
    CMTimeGetSeconds(self.player.currentItem.duration)) { 
    // Playback reached end 
} else if (!self.player.currentItem.playbackLikelyToKeepUp) { 
    // Not ready to play, wait until enough data is loaded 
} 

는 표시보기를 숨 깁니다. 주 스레드에서 표시기보기를로드하십시오.

dispatch_async(dispatch_get_main_queue(), ^{ 
//load spinner 
});