AVPlayer video from temporary directory after recording stops playing after few seconds
我有一个定制的相机,可以录制视频和拍照。问题是在录制并推送带有录制视频预览的 viewController 后,视频在几秒钟后停止播放。
所以流程是:
Custom camera opened; I've recorded a video; Controller with recorded video preview is shown. (by pushViewController(controller, animated: true)); Video started playing; After N seconds (1-2 sec) video stopped for some reason.
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | if let error = error { // handle error return } if let currentBackgroundRecordingID = backgroundRecordingID { backgroundRecordingID = UIBackgroundTaskIdentifier.invalid if currentBackgroundRecordingID != UIBackgroundTaskIdentifier.invalid { UIApplication.shared.endBackgroundTask(currentBackgroundRecordingID) } } // open viewController and play recently recorded video from temporary files. |
1 2 3 4 5 6 7 8 9 10 11 | let player = AVPlayer(url: url) player.actionAtItemEnd = .none let playerLayer = AVPlayerLayer(player: player) playerLayer.frame = self.view.frame playerLayer.videoGravity = .resizeAspectFill self.view.layer.insertSublayer(playerLayer, at: 0) player.seek(to: .zero) player.play() |
1 2 3 | let vc = TestController() vc.url = url! self.navigationController?.pushViewController(vc, animated: true) |
注意:一件奇怪的事情是,如果我将 pushViewController 与动画:false 一起使用,那么播放器将播放完整视频,如果动画:true,视频将在几秒钟后停止。此外,当前视图控制器工作正常。
注意 2:如果我使用 DispatchQueue 以 0.5 秒的延迟调用 setupVideoBackground(),视频将不会停止。
我不明白,为什么视频在
之后停止播放
解决方案 1:
所以问题出在相机屏幕上的
解决方案 2:
您可以将 AVAudioSession 类别设置为