我正在使用AVQueuePlayer按顺序播放视频(虽然我在播放之间打嗝)。 现在我想:
1)循环该序列中的最后一项(而不是完整项)。
2)这样做,我也想消除打嗝。
以下是我播放视频序列的代码。我怎么能实现我的2个目标?
[super viewDidLoad];
NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"vid_1" ofType:@"mov"];
NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"vid_2" ofType:@"mov"];
AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
[queuePlayer play];
我想到的另一种方法是播放1长视频并循环播放它的最后几秒。也许我也可以这样避免打嗝。这种方法是否可以实现?如果是这样,如何调整下面的代码(播放1个视频)?
[super viewDidLoad];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"vid_long" ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
avPlayer = [AVPlayer playerWithURL:fileURL];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];
[avPlayer play];
答案 0 :(得分:1)
以下是循环播放视频的解决方案: -
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
int k=0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(itemPlayEnded:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[queuePlayer currentItem]];
- (void)itemPlayEnded:(NSNotification *)notification
{
k++;
if(k<10-15 times)
{
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
}
编辑回答: - 上面的答案播放相同的文件,但这对我有用。 它在循环中播放最后添加的文件:
- (void)itemPlayEnded1:(NSNotification *)notification
{
NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"];
k=0;
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
// AVPlayerItem *p = [notification object];
NSMutableArray *currentItemArray = [NSMutableArray arrayWithObjects:secondVideoItem,nil];
AVQueuePlayer* queuePlayer = [[AVQueuePlayer alloc] initWithItems:currentItemArray];
[queuePlayer play];
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
avPlayer.actionAtItemEnd = AVPlayerItemStatusReadyToPlay;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(itemPlayEnded:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[queuePlayer currentItem]];
}
答案 1 :(得分:0)
我能找到的最佳方式是
观察AVQueuePlayer中的每个播放项目。
queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
for(AVPlayerItem *item in items) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(nextVideo:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item ];
}
每个nextVideo上的再次插入currentItem以将其排队以进行回放。确保为每个项目寻求零。在advanceToNextItem之后,AVQueuePlayer将从队列中删除currentItem。我们只是重新插入。
-(void) nextVideo:(NSNotification*)notif {
AVPlayerItem *currItem = notif.userInfo[@"object"];
[currItem seekToTime:kCMTimeZero];
[queuePlayer advanceToNextItem];
[queuePlayer insertItem:currItem afterItem:nil];
}
答案 2 :(得分:0)
我发现实现这一目标的唯一方法是采用Apple的Using AVQueuePlayer to demonstrate loop playback样本。您需要添加自己的代码才能播放初始视频,然后循环播放另一个视频,但该方法比我尝试的其他方法效果更好。