AVPlayer's video and audio become out of sync

2019-05-03 09:25发布

问题:

I'm using AVPlayer to play videos in my app. Video playback always works perfectly, except for when you pause/resume the video. If the user presses the pause button, and then resumes the video, sometimes the audio will be ahead of the video.

The video resumes at the correct location, but the audio is ahead. It's as if when you press pause, the audio keeps running.

When they press the pause button, all I am doing is calling the pause method of the AVPlayer, and I have also tried setting it's rate property to 0.0 which is supposed to be the exact same thing according to the documentation.

I only noticed this behavior after upgrading my iPhone 6 to iOS 9. I have tested this on an iPhone 6 Simulator using iOS 8.4 and I cannot recreate this behavior, but there are usually differences when using AVPlayer on a real device versus a simulator so I can't say for 100% sure that as of right now the bug can't be recreated on iOS 8 as well.

If you look at the iOS 9 release notes, and search for "AVFoundation" you will see that some things have changed: https://developer.apple.com/library/prerelease/ios/releasenotes/General/RN-iOSSDK-9.0/

I figured that AVPlayerItem's new property canUseNetworkResourcesForLiveStreamingWhilePaused was the source of this issue but after testing, it doesn't matter whether I set this to YES or NO, the issue still happens in both cases.

At this point I have no idea why this is happening, but as of right now my best guess is that this is an iOS 9 bug.

I'm hoping that I'm not the only one experiencing this, and that someone has found a solution already, or maybe has an idea as to why this is happening. I'm open to trying and testing anything.

Thanks for the help.