2

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?

JAL
  • 41,701
  • 23
  • 172
  • 300
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • any luck on this? I am facing same issue – Bhavesh Lathigara Aug 14 '14 at 08:07
  • 1
    Not exactly. I changed the player type. Now using `MPMoviePlayerController` instead of `AVPlayer`. – AnujAroshA Aug 14 '14 at 09:58
  • 1
    well actually I am using KXMovie player for my app and it will crash after 10 to 15 minutes due to memory pressure(Receive Memory Warning). but I have no solution right now. – Bhavesh Lathigara Aug 19 '14 at 09:41
  • 1
    Published the issue in [iOS developer forum](https://devforums.apple.com/thread/241430) with MPMoviePlayerController changes. – AnujAroshA Aug 19 '14 at 11:25
  • 1
    I find the solution of my code that is when memory pressure our -(void)didReceiveMemoryWarning call so I nil all the object that affect to playing audio and re create the objects when need so now its working fine for me – Bhavesh Lathigara Aug 20 '14 at 11:58
  • 1
    I'm doing the same thing instead of creating the object again, navigate back to a home screen. So, I set nil for the objects in `viewDidDisapear` method, not inside `didReceiveMemoryWarning`. I'll try your way too. Thank you very much for the tip. – AnujAroshA Aug 21 '14 at 03:43
  • your welcome anytime – Bhavesh Lathigara Aug 21 '14 at 10:31
  • Having this problem myself. Nil'ing the pointers to the YTPlayerView instance, but it's not letting go of the memory. In fact, it just keeps allocating each time I open up the VC it's contained in again. – Micrified Nov 07 '15 at 14:48

1 Answers1

2

Moving Bhavesh Lathigara's solution in the comments to a Community Wiki answer:

When didReceiveMemoryWarning is called, nil all the objects that are related to video/audio playback and re-create them when needed again.

Community
  • 1
  • 1
JAL
  • 41,701
  • 23
  • 172
  • 300