AVPlayer plays audio but video freezes

I have two

UIViewControllers
each with an
AVPlayer
that are supposed to play a video file when they are pushed into a
UINavigationController
:


    -(void)viewWillAppear:(BOOL)animated{
  
    videoView = [[UIView alloc]initWithFrame:self.view.frame];
  
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mov"];
    NSURL *fileURL = [NSURL fileURLWithPath:filepath];
    self.avPlayer = [AVPlayer playerWithURL:fileURL];
  
    AVPlayerLayer *foregroundLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
    self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
    foregroundLayer.frame = self.view.frame;
    foregroundLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  
    [videoView.layer addSublayer:foregroundLayer];
  
    [self.view addSubview:videoView];
  
    self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
  
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(playerItemDidReachEnd:)
    name:AVPlayerItemDidPlayToEndTimeNotification
    object:[self.avPlayer currentItem]];
    }
  
    -(void)viewDidAppear:(BOOL)animated{
  
    [self.avPlayer play];
    }
  
    -(void)playerItemDidReachEnd:(NSNotification *)notification {
  
    [self.navigationController popViewControllerAnimated:NO]
    }


The first time I push any of the

UIViewControllers
the playback works well. But after that, if I push any of them again the sound plays but the video freezes.

I've tried using a

MPMoviePlayerViewController
but the behavior is the same. Any thoughts?

Replies

Did you ever find a solution to this? I am struggling with my AVPlayer worling fine,even looping, but when I go back and come back in, only the audio plays. This is happening elsewhere as well in a UICollectionViewCell

I'm having this exact same issue. Did either of you have any luck with this?

I'm also having the same issue when streaming a remote mp4 file and download it at the same time.

hopely , apple engineer would share a solution.

I recently noticed this issue as well when playing assets downloaded to the device. Playback starts fine, but when i pause and then attempt to resume playback the audio resumes but the video does not and the rest of the UI becomes unresponsive. the effect seems much more pronounced with large files (almost 3Gig), but i also see short freezes on smaller files. in the case of the 3Gig asset the freeze will last upwards of 30 seconds before the UI becomes responsive once again.


iOS 10.3.3

multiple devices

Yes this happens for me too, unfortunately in iOS 11 also. Only on poor connections, it will play a bit of the video, and then freeze on the same frame and continue playing the audio. Very annoying!

Did anyone find a solution? It happens (as said by jdtech) on iOS 11 too, and it is really annoying! We can't even handle the freez, so I don't know how to do (I live stream an m3u8 streaming). Thanks!

This issue comes up when in a bad network environment. Because of that, the AVPlayer will try to switch to the low bandwidth manifest which can be an audio one.


You can observe the currentItem.tracks to know when it will happen.

If it happens, it will lose the video track, but remain the audio track. You can detect the issue by this.


The AVPlayer also will try to take back the video stream later. But the issue I have is when the video stream came in, I don't know when the AVPlayer will render it. Or how to force the AVPlayer to render it.

Did anyone figure out this issue? I am facing similar issue. When I pause and play video on same AVPlayerLayer, it works perfectly fine. However, when I try to display same AVPlayer item on AVPlayerViewController, only audio plays while video freeazes.

This is not exactly the same issue as the original one, but I'm having the one you are describing. I found a work around but it's very ugly imo.


When you observe that the AVPlayer is getting back to a video enabled track, you can seek to a position that hasn't been buffered yet (I tried 30seconds ahead), and then seek back to your current position, and normaly the images is rendered well.
I guess it has to be about the buffers, but my knowledge in those parts of iOS is too poor to understand what's going on and maybe find a better solution.
Have you ever found a better solution?

I'm having the same issue as well. Any updates on an answer?

I am also having this issue and apparently, WhatsApp has the same issue too. I often encountered that on my iPhone. If anyone fixes this, please reply with a solution here on this post. Thanks!!!

I faced the same issue.

Video freezes and audio continues to play

I fixed by adding this line try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)

my code for reference:

                try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
                if self.playerLayer != nil {
                    self.playerLayer?.removeFromSuperlayer()
                    self.playerLayer = nil
                }
                self.playerLayer = AVPlayerLayer(player: self.player)
                self.playerLayer?.videoGravity = .resize
                self.player = AVPlayer(url: videoUrl)
                self.playerLayer?.frame = self.view.layer.bounds
                addObserversVideoItem(myCustomVideoItem)