我正在与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
什么都不做。我把它移到了选项栏:try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: [AVAudioSessionCategoryOptions.MixWithOthers])
工作!
你可以参考这个问题的答案
我已经使用SCRecorder库实现了相同的功能,但也可以用AVCaptureSession实现。
这是我在swift中工作的东西,它本质上和jason的答案是一样的,但是对于swift。
将此代码添加到
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?){
add your laucher code
let audioSession = AVAudioSession.sharedInstance()
do {
// Set the audio session category, mode, and options.
try audioSession.setCategory(.playAndRecord, options: [.mixWithOthers,.defaultToSpeaker,.allowBluetooth])
try audioSession.setActive(true)
} catch {
print("Failed to set audio session category.")
}
}
然后在您设置capturesession的文件中
captureSession.automaticallyConfiguresApplicationAudioSession = false
本质上是mixwithother工作,正如它在文档中所说的"选项,表明是否从这个会话的音频与其他音频应用程序中的活动会话的音频混合"(https://developer.apple.com/documentation/avfoundation/avaudiosession/categoryoptions),默认扬声器允许音乐在与其他选项混合时发出更大的声音。