It\'s my first question here, so don\'t be severe.
I\'m playing video from the net using AVPlayer. I output the current frame using AVPlayerItemVideoOutput
Code blow does not solve my problem, I still got nothing from [AVPlayerItemVideoOutput hasNewPixelBufferForItemTime]
if (failedCount > 100) {
failedCount = 0;
[_playerItem removeOutput:_output];
[_playerItem addOutput:_output];
}
Finally after testing my code for a whole day. I found a way to solve it.
#pragma mark - AVPlayerItemOutputPullDelegate
- (void)outputMediaDataWillChange:(AVPlayerItemOutput *)sender {
if (![self.videoOutput hasNewPixelBufferForItemTime:CMTimeMake(1, 10)]) {
[self configVideoOutput];
}
[self.displayLink setPaused:NO];
}
Check [AVPlayerItemVideoOutput hasNewPixelBufferForItemTime]
when outputMediaDataWillChange:
called. Recreate your AVPlayerItemVideoOutput
if no new pixel buffer at 0.1s.
Code in [self configVideoOutput];
just recreate a new AVPlayerItemVideoOutput
to replace current videoOutput
property.
Why 0.1s?
I tested and experimented many times I found that first 1 or 2 frame may always get no pixel buffer. So first 1/30s, 2/30s (for video at 30fps) may have no frame and pixel buffer. But if no video pixel buffer after 0.1s, the video output may broken or something problem with it. So we need recreate it.