在我的應用程序中,我應該立即播放第一個視頻,然後播放第二個視頻,這將重複播放。所以,起初我使用了MPMoviePlayerController,但是兩個視頻之間已經有了一個黑屏。所以,我在某處讀過,在我的情況下,我應該使用AVFoundation。那是我用AVQueuePLayer與兩個AVPLayerItem,我用我的NSURL創建的。但問題是,在第一個視頻結束後,會有很長的停頓。接近0.5秒,然後開始第二次視頻。如何刪除它?使用什麼方式?所以,我非常感謝任何幫助!我非常需要它,儘快。謝謝!在iOS中按順序播放兩個視頻,並在它們之間留下任何間隙或停頓5
這裏所有我的方法,我用:
- (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 ];
}
}