AVAudioSession 配置给别人录音播放
AVAudioSession configuration to record and play with others
我想配置 AVAudioSession 以便我可以录制带音频的视频并播放音乐应用程序(或任何其他产生声音的应用程序,通常是互联网收音机应用程序)中的音乐
我这样配置会话:
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation)
当我的录音应用程序是 运行 时,我可以开始播放音乐应用程序中的音频并录制视频(也录制音频)。目前还不错。
现在,我将录音应用程序置于后台。音乐应用程序继续播放。没事。但是,当我的应用程序从背景音乐应用程序返回时停止播放。我希望它继续下去。所以它在拍摄视频时基本上就像内置相机应用程序一样。有没有办法告诉系统我想让其他应用继续播放?
我几乎遇到过类似的问题,我花了一两天的时间来解决这个问题。
在我的例子中,我使用 AVCaptureSession
进行 video/audio 录音,它有 属性 调用 automaticallyConfiguresApplicationAudioSession
。根据 Apple 文档
automaticallyConfiguresApplicationAudioSession: A Boolean value that indicates whether the capture session automatically changes
settings in the app’s shared audio session.
The value of this property defaults to true, causing the capture
session to automatically configure the app’s shared AVAudioSession
instance for optimal recording
因此,如果我们手动设置 AVAudioSession
,我们应该将此 属性 设置为 false
,如下所示,
captureSession.automaticallyConfiguresApplicationAudioSession = false;
将此 属性 设置为 false
并手动设置适当的 AVAudioSession
类别已经解决了我的问题!希望对你的情况也有帮助!
对您的代码的更多观察
- 激活音频不需要
.notifyOthersOnDeactivation
标志
会话 此标志仅在停用音频会话时使用。那就是当你在 setActive(_:options:)
实例方法的 beActive 参数中传递一个 false
的值时。所以下面的代码可以很好地激活一个会话
try? AVAudioSession.sharedInstance().setActive(true)
默认 AVAudioSessionCategoryPlayAndRecord
类别领先
到非常低音量的背景声音。如果你想要正常音量,需要使用选项 .defaultToSpeaker
和 .mixWithOthers
如下,
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .defaultToSpeaker])
好的!那么我认为如果您已正确完成所有其他操作,它现在可以正常工作。
注意** 我创建了一个工作示例项目供您参考,您可以在 GitHub Here
上找到它
我想配置 AVAudioSession 以便我可以录制带音频的视频并播放音乐应用程序(或任何其他产生声音的应用程序,通常是互联网收音机应用程序)中的音乐
我这样配置会话:
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation)
当我的录音应用程序是 运行 时,我可以开始播放音乐应用程序中的音频并录制视频(也录制音频)。目前还不错。
现在,我将录音应用程序置于后台。音乐应用程序继续播放。没事。但是,当我的应用程序从背景音乐应用程序返回时停止播放。我希望它继续下去。所以它在拍摄视频时基本上就像内置相机应用程序一样。有没有办法告诉系统我想让其他应用继续播放?
我几乎遇到过类似的问题,我花了一两天的时间来解决这个问题。
在我的例子中,我使用 AVCaptureSession
进行 video/audio 录音,它有 属性 调用 automaticallyConfiguresApplicationAudioSession
。根据 Apple 文档
automaticallyConfiguresApplicationAudioSession: A Boolean value that indicates whether the capture session automatically changes settings in the app’s shared audio session.
The value of this property defaults to true, causing the capture session to automatically configure the app’s shared AVAudioSession instance for optimal recording
因此,如果我们手动设置 AVAudioSession
,我们应该将此 属性 设置为 false
,如下所示,
captureSession.automaticallyConfiguresApplicationAudioSession = false;
将此 属性 设置为 false
并手动设置适当的 AVAudioSession
类别已经解决了我的问题!希望对你的情况也有帮助!
对您的代码的更多观察
- 激活音频不需要
.notifyOthersOnDeactivation
标志 会话 此标志仅在停用音频会话时使用。那就是当你在setActive(_:options:)
实例方法的 beActive 参数中传递一个false
的值时。所以下面的代码可以很好地激活一个会话try? AVAudioSession.sharedInstance().setActive(true)
默认
AVAudioSessionCategoryPlayAndRecord
类别领先 到非常低音量的背景声音。如果你想要正常音量,需要使用选项.defaultToSpeaker
和.mixWithOthers
如下,try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .defaultToSpeaker])
好的!那么我认为如果您已正确完成所有其他操作,它现在可以正常工作。
注意** 我创建了一个工作示例项目供您参考,您可以在 GitHub Here
上找到它