NSNotificationCenterとplayerItemDidReachEndを使用して、AVFoundationビデオで再生したビデオをループします。
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
[self.avPlayer addObserver:self forKeyPath:@"status"
options:0 context:AVMoviePlayerViewControllerStatusObservationContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
[self.VideoView setPlayer:self.avPlayer];
と
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[notification.object seekToTime:kCMTimeZero];
[self startVideoLoop];
}
うまく機能しますが、残念ながら信頼性はありません。論理的な理由がない場合もありますが(私にはそう思われます)、プレーヤーは映画の最後で再起動せずに停止します。
NSNotificationCenterがムービーの終わりを見逃したり、メソッドplayerItemDidReachEndが通知メッセージを見逃したりする可能性はありますか?ばかげた質問。でも頭も尻尾も作れません...