录制后临时目录中的 AVPlayer 视频在几秒钟后停止播放
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.
代码:
- didFinishRecordingTo outputFileURL:
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.
- setupVideoBackground() -> AVPlayer 的配置,在 viewDidLoad/viewWillAppear:
中调用
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()
- 推送视图控制器:
let vc = TestController()
vc.url = url!
self.navigationController?.pushViewController(vc, animated: true)
注意:一件奇怪的事情是,如果我将 pushViewController 与 animated: false 一起使用,则播放器将播放完整视频,如果与 animated: true 一起使用,则视频将在几秒钟后停止。此外,当前视图控制器工作完美。
注意 2:如果我使用 DispatchQueue 延迟 0.5 秒调用 setupVideoBackground(),视频将不会停止。
我不明白,为什么视频在 pushViewController(vc, animated: false)
之后可以正常播放,而在 pushViewController(vc, animated: true)
之后就停止播放了
解决方案一:
所以问题出在相机屏幕上的 viewDidDissapear
中。有一个cameraSession.stopSession()
。每次调用 stopSession 时,avplayer 都会停止播放。我不知道 AVCaptureSession.stopSession()
为什么以及如何与 AVPlayer
相关,但是从 viewDidDissapear
中删除 cameraSession.stopSession()
解决了问题。
方案二:
您可以将 AVAudioSession 类别设置为 .playAndRecord
并且视频将正确播放:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoRecording, options: [.mixWithOthers])
我有一个自定义相机,可以录制视频和拍照。问题是在使用录制的视频预览录制并推送 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.
代码:
- didFinishRecordingTo outputFileURL:
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.
- setupVideoBackground() -> AVPlayer 的配置,在 viewDidLoad/viewWillAppear: 中调用
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()
- 推送视图控制器:
let vc = TestController()
vc.url = url!
self.navigationController?.pushViewController(vc, animated: true)
注意:一件奇怪的事情是,如果我将 pushViewController 与 animated: false 一起使用,则播放器将播放完整视频,如果与 animated: true 一起使用,则视频将在几秒钟后停止。此外,当前视图控制器工作完美。
注意 2:如果我使用 DispatchQueue 延迟 0.5 秒调用 setupVideoBackground(),视频将不会停止。
我不明白,为什么视频在 pushViewController(vc, animated: false)
之后可以正常播放,而在 pushViewController(vc, animated: true)
解决方案一:
所以问题出在相机屏幕上的 viewDidDissapear
中。有一个cameraSession.stopSession()
。每次调用 stopSession 时,avplayer 都会停止播放。我不知道 AVCaptureSession.stopSession()
为什么以及如何与 AVPlayer
相关,但是从 viewDidDissapear
中删除 cameraSession.stopSession()
解决了问题。
方案二:
您可以将 AVAudioSession 类别设置为 .playAndRecord
并且视频将正确播放:
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoRecording, options: [.mixWithOthers])