我正在開發一個記錄視頻片段的應用程式(請參閱 Instagram 的卷軸以供參考),并且我目前正在處理應用程式移動到后臺/螢屏被鎖定時的狀態。
我當前遇到的問題是,如果我在正在進行的視頻錄制期間將應用程式移至后臺/鎖定螢屏,則 AVCaptureFileOutputRecordingDelegate 的 fileOutput 方法無法保存視頻。我曾嘗試在 .plist 中添加“所需的背景模式”,并且我還偶然發現了以下執行緒,現在我不確定是否可以在應用程式移動到時實際保存正在進行的視頻錄制背景,以及如果您想遵守隱私準則,這樣做是否是個好主意。
我想知道是否有辦法延遲將應用程式移至后臺,以便我可以執行通常用于在設備進入后臺之前停止和保存視頻錄制的方法(最終失敗為我保存視頻的程序)。
注意:將我保存/停止正在進行的視頻錄制的方法放在以下觀察中不起作用,它將如上所述失敗:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
print("Moving to the background!")
toggleRecording() <--- this normally stops and saves the recording but it fails here.
}
感謝您提供的任何意見!
uj5u.com熱心網友回復:
研究場景協議。
我參考...
場景協議提供場景修飾符,定義為具有默認實作的協議方法,可用于配置場景。例如,您可以使用 onChange(of:perform:) 修飾符在值更改時觸發操作。當視窗組中的所有場景都移到后臺時,以下代碼清空快取:
并且要清楚這是Apple提供的示例......
struct MyScene: Scene {
@Environment(\.scenePhase) private var scenePhase
@StateObject private var cache = DataCache()
var body: some Scene {
WindowGroup {
MyRootView()
}
.onChange(of: scenePhase) { newScenePhase in
if newScenePhase == .background {
cache.empty()
}
}
}
}
對于您的情況,您可以將cache.empty命令替換為toggleRecording().
uj5u.com熱心網友回復:
以下代碼解決了我的特定情況下的問題:
當應用程式被置于后臺時,會AVCaptureFileOutputRecordingDelegate被呼叫并且不可避免地會發生錯誤。如果發生這種情況,我會告訴應用程式從var activeRecordingUrl: URL?我在開始錄制時生成/更新的 url ( ) 中查找任何錄制的資料。如果找到了資料(對我來說似乎總是如此),我會像我一樣存盤它,就像用戶從 UI“手動”停止記錄一樣。
extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
func fileOutput(
_ output: AVCaptureFileOutput,
didFinishRecordingTo outputFileURL: URL,
from connections: [AVCaptureConnection],
error: Error?
) {
guard error == nil else {
// --------- Start of fix ---------
NSLog("> ERROR: Recording was interrupted: \(String(describing: error?.localizedDescription)), attempting to salvage record data from activeRecordingUrl.")
guard let outputUrl = activeRecordingUrl else {
NSLog("> ERROR: found nil when unwrapping activeRecordingUrl.")
onRecordingFinished(false)
return
}
recordings.append(VideoSegment(avAssetUrl: outputUrl, cameraPosition: sessionInput.device.position, avAsset: AVAsset(url: outputUrl)))
NSLog("Found record data from the session that was interrupted.")
onRecordingFinished(true)
// --------- End of fix ---------
return
}
recordings.append(VideoSegment(
avAssetUrl: outputFileURL,
cameraPosition: sessionInput.device.position,
avAsset: AVAsset(url: outputFileURL
)))
onRecordingFinished(true)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/344915.html
上一篇:錯誤ITMS-90360:缺少Info.plist值
下一篇:kafka生產者實作細節
