我可以测试IOS4 AVPlayer seekToTime何时完成?

问题描述:

我使用AVFoundation实现一个AVPlayer。我想循环的视频剪辑不断,所以我注册一个AVPlayerItemDidPlayToEndTimeNotification调用这个方法:我可以测试IOS4 AVPlayer seekToTime何时完成?

- (void)player1ItemDidReachEnd:(NSNotification *)notification 
{ 
dispatch_async(dispatch_get_main_queue(), 
     ^{ 
     [player1 seekToTime:kCMTimeZero]; 
     [player1 play]; 
     }); 
} 

它的工作原理了一些时间,但最终会停止播放,大概是因为seekToTime的异步完成的。我怎样才能使这个代码防弹?

我现在已经通过将AVPlayer的AVPlayerActionAtItemEnd属性设置为AVPlayerActionAtItemEndNone来解决此问题 - 默认为AVPlayerActionAtItemEndPause。 AVPlayer不再在剪辑结束时暂停,因此游戏不必重新开始。还发现不需要将seekToTime分派给主队列。

中或者Controller.h添加

AVPlayer *myplayer; 
在Controller.m或者

添加

#import <CoreMedia/CoreMedia.h> 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerItemDidReachEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:[myplayer currentItem]]; 
[myplayer play]; 

,并从选择

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
AVPlayerItem *playerItem = [notification object]; 
[playerItem seekToTime:kCMTimeZero]; 
[myplayer play]; 

}添加功能

这是帮助我循环播放视频,但你需要缓存是一些如何(平滑环)

+0

嘿,一段时间后停止%(播放时,播放次数为2,5,6,11 :( – iTux 2011-02-07 09:44:43

- (void)playLogo { 
NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" 
               ofType:@"m4v" 
              inDirectory:@"../Documents"]; 

self.myplayerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]]; 
self.myplayer = [AVPlayer playerWithPlayerItem:self.myplayerItem]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerItemDidReachEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
              object:[self.myplayer currentItem]]; 
[videoView setPlayer:self.myplayer]; 
[videoView setAlpha:0.5f]; 
[self.myplayer play]; } 

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
[videoView setAlpha:0.0f]; 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:AVPlayerItemDidPlayToEndTimeNotification 
               object:[self.myplayer currentItem]]; 
[self playLogo]; } 

player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) 
name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]]; 

(void)playerItemDidReachEnd:(NSNotification *)notification { 
AVPlayerItem *p = [notification object]; 
[p seekToTime:kCMTimeZero]; 
}