gpt4 book ai didi

ios - 将 AVCaptureDeviceInput 添加到 CaptureSession 会重置/重新聚焦视频

转载 作者:IT王子 更新时间:2023-10-29 05:06:52 25 4
gpt4 key购买 nike

一个视频录制应用程序。我希望它在不停止/暂停背景音乐的情况下工作(例如,当用户收听 Apple Music 时)。这一点我可以很好地在 AVAudioSession 单例上将类别设置为 mixWithOthers

设置类别后,我还需要将 AVCaptureDeviceInput 添加到 AVCaptureSession(以便录制音频)。这会导致背景音频出现故障/打嗝,还会导致视频重置/重新聚焦。

我已经调查过,背景音频故障似乎是无法避免的,但添加输入时视频不应自行重置。视频重置的结果是录制的视频的第一帧是暗/黑或从失焦帧开始然后聚焦。

还检查了 Snapchat ios 应用程序,他们在开始录制时也有音频故障,但视频开始录制流畅。我做错了什么。

我的代码:

//Setting audio session to mixWithOthers upon startup
let session = AVAudioSession.sharedInstance()

do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
with: [.mixWithOthers])
if session.mode != AVAudioSessionModeVideoRecording {
try session.setMode(AVAudioSessionModeVideoRecording)
}
} catch let error {
print("avsession category error: \(error)")
}

然后:

//Just before recording starts will add audio input
let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
do
{
let deviceInput = try AVCaptureDeviceInput(device: audioDevice) as AVCaptureDeviceInput
if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
}
else {
print("Could not add audio device input to the session")
}


}
catch let error as NSError {
print(error.localizedDescription)
return
}

有没有可能完全没有任何故障地做到这一点?如果不是,那我怎样才能让它至少像 Snapchat(调用 addInput 时没有视频重置)?

最佳答案

我注意到 AVCaptureSession addInput 重置了由 AVAudioSession AVAudioSessionCategory(Solo)Ambient 类别设置的静音

 Category                              Silenced   Interrupts
AVAudioSessionCategoryAmbient Yes No
AVAudioSessionCategorySoloAmbient Yes Yes
AVAudioSessionCategoryMultiRoute No Yes
AVAudioSessionCategoryPlayAndRecord No Yes
AVAudioSessionCategoryPlayback No Yes
AVAudioSessionCategoryRecord No Yes

https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

When the user moves the Silent switch (or Ring/Silent switch on iPhone) to the “silent” position, your audio is silenced.

AVAudioSessionCategoryAmbient—Playback only. Plays sounds that add polish or interest but are not essential to the app’s use. Using this category, your audio is silenced by the Ring/Silent switch and when the screen locks.

AVAudioSessionCategorySoloAmbient—(Default) Playback only. Silences audio when the user switches the Ring/Silent switch to the “silent” position and when the screen locks. This category differs from the AVAudioSessionCategoryAmbient category only in that it interrupts other audio.

关于ios - 将 AVCaptureDeviceInput 添加到 CaptureSession 会重置/重新聚焦视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42950056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com