gpt4 book ai didi

ios - AVQueuePlayer:循环队列中的最后一项(并消除打嗝)或循环视频的最后几秒

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:51 25 4
gpt4 key购买 nike

我正在使用 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];

最佳答案

这是循环播放视频的解决方案:-

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]];

}

关于ios - AVQueuePlayer:循环队列中的最后一项(并消除打嗝)或循环视频的最后几秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14765990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com