Reputation: 4811
In my iOS application, I am running a YouTube video as a loop with the help of iOS YouTube helper library. But I am not giving the opportunity to play the video at it's full length but after 20 seconds I am queued the same video again like below.
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{
if (state == kYTPlayerStateQueued) {
startedTimer = NO;
[self.playerView playVideo];
} else if (state == kYTPlayerStatePlaying) {
if (!startedTimer) {
startedTimer = YES;
vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
}
}
}
and
- (void)restartVideo {
[self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}
That's working perfectly as I wanted.
Next I wanted to play mp4 file before YouTube replay the video in every time. To achieve that I have used AVPlayer. Then the code have changed like below.
- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{
if (state == kYTPlayerStatePlaying) {
if (self.avPlayer != nil) {
avPlayerLayer.hidden = YES;
self.avPlayer = nil;
}
if (!startedTimer) {
startedTimer = YES;
vidReplayTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(restartVideo) userInfo:nil repeats:NO];
}
}
}
and
- (void)restartVideo {
self.avPlayer = [AVPlayer playerWithURL:introVideoFileURL];
avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
avPlayerLayer.frame = CGRectMake(0, 0, 320, 330);
[self.view.layer addSublayer: avPlayerLayer];
[self.avPlayer play];
[self.playerView cueVideoById:selectedYTVideoId startSeconds:0.1 suggestedQuality:kYTPlaybackQualityMedium];
}
After above changes the app runs as I expected but nearly four minutes of running it gives "Terminated due to Memory Pressure" pop up window in my Xcode and app crashes. I have checked the memory with Instruments developer tool and app uses nearly 35 MB of memory by the time it crashes. By the time app started to running, it uses more that 45 MB of memory and running smoothly.
As you can see, I'm creating the AVPlayer only when I need it and set it nil just after completing the work. What can be the reason for this issue and how can I solve this?
Upvotes: 2
Views: 1542