gpt4 book ai didi

ios - 在不停止设备声音/音乐的情况下录制视频

转载 作者:IT王子 更新时间:2023-10-29 05:14:41 28 4
gpt4 key购买 nike

我正在使用 Swift 的 AVFoundation 在 iOS 中捕捉视频。但是当我使用 Apple Music/Spotify 播放歌曲然后单击我的应用程序的录制按钮时,它会暂停/停止音乐然后录制视频。我如何防止这种情况发生?

这是我的代码:

@IBAction func record_video(sender: AnyObject) {
var initialOutputURL = NSURL(fileURLWithPath: "")

do
{
initialOutputURL = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent("output").URLByAppendingPathExtension("mov")
}catch
{
print(error)
}
if !isRecording
{

isRecording = true
if let outputs = captureSession.outputs as? [AVCaptureOutput] {
for output in outputs {
captureSession.removeOutput(output)
}
}

do
{
try audioSession.setCategory(AVAudioSessionCategoryAmbient)
}
catch
{
print("Can't Set Audio Session Category: \(error)")
}
AVAudioSessionCategoryOptions.MixWithOthers

do
{
try audioSession.setMode(AVAudioSessionModeVideoRecording)
}
catch
{
print("Can't Set Audio Session Mode: \(error)")
}
// Start Session
do
{
try audioSession.setActive(true)
}
catch
{
print("Can't Start Audio Session: \(error)")
}


UIView.animateWithDuration(0.5, delay: 0.0, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations: { () -> Void in
self.record.transform = CGAffineTransformMakeScale(0.75, 0.75)
}, completion: nil)

let audioInputDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
do
{
let audioInput = try AVCaptureDeviceInput(device: audioInputDevice)

// Add Audio Input
if captureSession.canAddInput(audioInput)
{
//captureSession.addInput(audioInput)
}
else
{
NSLog("Can't Add Audio Input")
}

let videoInput: AVCaptureDeviceInput
do
{
videoInput = try AVCaptureDeviceInput(device: captureDevice)

// Add Video Input
if captureSession.canAddInput(videoInput)
{
captureSession.addInput(videoInput)
}
else
{
NSLog("ERROR: Can't add video input")
}
}
catch let error
{
NSLog("ERROR: Getting input device: \(error)")
}
videoFileOutput = AVCaptureMovieFileOutput()
captureSession.addOutput(videoFileOutput)
captureSession.sessionPreset = AVCaptureSessionPresetHigh
captureSession.automaticallyConfiguresApplicationAudioSession = false

videoFileOutput?.startRecordingToOutputFileURL(initialOutputURL, recordingDelegate: self)


}
catch let error
{
NSLog("Error Getting Input Device: \(error)")
}

}
else
{
isRecording = false

UIView.animateWithDuration(0.5, delay: 0, options: [], animations: { () -> Void in
self.record.transform = CGAffineTransformMakeScale(1.0, 1.0)
}, completion: nil)
record.layer.removeAllAnimations()
videoFileOutput?.stopRecording()
}



}

请注意,我注释掉了 captureSession.addInput(audioInput)。如果我删除该代码,该应用程序能够录制视频,它不会暂停/停止音乐,但视频输出没有声音。有解决办法吗?

最佳答案

我自己设法解决了这个问题。该行:AVAudioSessionCategoryOptions.MixWithOthers 不执行任何操作。我将其移至选项:试试 audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers])

成功了!

关于ios - 在不停止设备声音/音乐的情况下录制视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874497/

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