私はavfoundationビデオプレーヤー内でビデオをループしています:
NSString *loopPath = @"SubView/introCycle";
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:loopPath withExtension:@"mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:^{
// The completion block goes here.
//NSLog(@"asset loaded asynchronously completed!");
// Completion handler block.
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (![self avPlayer] && status == AVKeyValueStatusLoaded) {
if(![self playerItem]){
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[self.playerItem addObserver:self forKeyPath:@"status" options:0 context:AVMoviePlayerViewControllerStatusObservationContext];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
}
self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
[self.VideoLoopView setPlayer:self.avPlayer];
}
else {
// You should deal with the error appropriately.
NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
}
});
}];
ループの終わりに、次のメソッドはビデオを再開します
int countRounds = 0;
- (void)playerItemDidReachEnd:(NSNotification *)notification {
*//block 1 - this is what we like*
if (notification.object == self.playerItem) {
countRounds++;
[self.playerItem seekToTime:kCMTimeZero];
[self.avPlayer play];
}
}
*// block 2 - this we don't like*
else {
NSInteger reason = [[notification.userInfo objectForKey:AVPlayerItemDidPlayToEndTimeNotification] integerValue];
NSLog(@"reason: %i", reason);
}
NSLog(@"playerItemDidReachEnd, starting round %i", countRounds);
}
これは正常に動作します。ブロック1のみが実行されます。ログショー
MainView did load
playerItemDidReachEnd, starting round 1
playerItemDidReachEnd, starting round 2
...
次に、別のビューをロードして、ビデオループビューに戻ります。メインビューを離れるとき
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
[self.avPlayer pause];
self.avPlayer = nil;
self.playerItem = nil;
[super viewDidDisappear:animated];
NSLog(@"MainView DidDisappear");
}
メインビューをリロードすると、ビデオがリロードされ、ループが正常に再生されます。しかし、ループの最後に、ブロック1とブロック2の両方が実行され、ログに記録されます。
MainView did load (from old log above)
playerItemDidReachEnd, starting round 1 *(from old log above)*
playerItemDidReachEnd, starting round 2 *(from old log above)*
MainView DidDisappear *(leaving the main view and loading a new one)*
MainView did load *(coming back to main view)*
reason: 0 *(block 2 is executed, triggered by what exactly? WHERE DOES THIS COME FROM?)*
playerItemDidReachEnd, starting round 2
playerItemDidReachEnd, starting round 3 *(this comes from block 1 - we like)*
reason: 0
playerItemDidReachEnd, starting round 3
playerItemDidReachEnd, starting round 4
reason: 0
playerItemDidReachEnd, starting round 4
playerItemDidReachEnd, starting round 5
したがって、ループの最後に、playerItemDidReachEndメソッドが2回トリガーされます。何故ですか?これはどこから来たのですか?