0

In my app, I should play firstvideo at once, and then play second video, which will on repeat. So, at first I used MPMoviePlayerController, but there already was a black screen between two videos. So, I read somewhere, that in my case I should use AVFoundation. That's whe I use AVQueuePLayer with two AVPLayerItem, which I create with my NSURL. But issue is, that after first video finished, there is a long pause. Nearly for 0.5 sec, before second vide o starts. How remove it? What way to use? So, I really appreciate any help! I need it very much, as soon as possible. Thanks!

Here all my methods which I use:

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

}

Aliaksandr B.
  • 146
  • 16

1 Answers1

0

Yes, multiple AVPlayer objects are your best option.

I solved this problem with a main timer function which monitored the current playback time of firstVideoItem, and when it was 0.01 seconds from the end, I would flip the players on the alpha channel, so secondVideoItem was visible, then play secondVideoItem.

I also used [secondVideoItem seekToTime:CMTimeMakeWithSeconds(1,1)] before issuing the play command to make sure the second video was initialised and ready to play. This helped a lot.

ben_the_builder
  • 309
  • 2
  • 8