0

在我的应用程序中,我应该一次播放第一个视频,然后播放第二个视频,这将重复。所以,一开始我用的是 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 ];
}

}

4

1 回答 1

0

是的,多个 AVPlayer 对象是您的最佳选择。

我用一个监控firstVideoItem当前播放时间的主定时器函数解决了这个问题,当距离结束0.01秒时,我会在alpha通道上翻转播放器,所以secondVideoItem可见,然后播放secondVideoItem。

在发出播放命令之前,我还使用了 [secondVideoItem seekToTime:CMTimeMakeWithSeconds(1,1)] 以确保第二个视频已初始化并准备好播放。这有很大帮助。

于 2012-03-27T10:03:49.640 回答