在我的应用程序中,我应该一次播放第一个视频,然后播放第二个视频,这将重复。所以,一开始我用的是 MPMoviePlayerController,但是两个视频之间已经出现了黑屏。所以,我在某处读到,就我而言,我应该使用 AVFoundation。这就是我使用 AVQueuePLayer 和两个 AVPLayerItem 的原因,我用我的 NSURL 创建了它们。但问题是,在第一个视频结束后,会有很长的停顿。大约 0.5 秒,在第二个视频 o 开始之前。怎么去掉?用什么方法?所以,我真的很感激任何帮助!我非常需要它,尽快。谢谢!
这是我使用的所有方法:
- (void) configureAVPlayer {
self.queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.queuePlayer];
self.isFirstVideoFinished = NO;
[self playVideos];
}
- (void) playVideos {
[self setPlayerItemsForQueue];
[self.videoHolder setPlayer:self.queuePlayer];
}
- (void) setPlayerItemsForQueue {
NSURL *fileURL = [[NSBundle mainBundle]
URLForResource:[self getStringForMode:self.currentMode] withExtension:@"mp4"];
NSMutableString *secondFile = [self getStringForMode:self.currentMode];
[secondFile appendString:@"Second"];
NSURL *secondFileUrl = [[NSBundle mainBundle] URLForResource:secondFile withExtension:@"mp4"];
AVAsset *firstAsset = [AVAsset assetWithURL:fileURL];
AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithAsset:firstAsset];//[AVPlayerItem playerItemWithURL:fileURL];
AVAsset *secondAsset = [AVAsset assetWithURL:secondFileUrl];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithAsset:secondAsset];//[AVPlayerItem playerItemWithURL:secondFileUrl];
self.queuePlayer = [AVQueuePlayer queuePlayerWithItems: [NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];
for (AVPlayerItem *playerItem in self.queuePlayer.items) {
[playerItem addObserver: self forKeyPath: @"status" options:0 context:NULL];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
dispatch_async(dispatch_get_main_queue(), ^{
if ([(AVPlayerItem *)object status] == AVPlayerItemStatusReadyToPlay) {
[self.queuePlayer play];
}
});
}
- (void) playerItemDidReachEnd:(NSNotification*) notification {
NSMutableString *secondFile = [self getStringForMode:self.currentMode];
[secondFile appendString:@"Second"];
NSURL *secondFileUrl = [[NSBundle mainBundle] URLForResource:secondFile withExtension:@"mp4"];
AVPlayerItem *playerItem = [[ AVPlayerItem alloc ] initWithURL:secondFileUrl];
if ([self.queuePlayer canInsertItem:playerItem afterItem:[notification object]]) {
[self.queuePlayer insertItem: playerItem afterItem: nil ];
}
}