AVAudioSession配置可与他人一起录制和播放 [英] AVAudioSession configuration to record and play with others

查看:100
本文介绍了AVAudioSession配置可与他人一起录制和播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置AVAudioSession,以便可以录制带有音频的视频,还可以播放音乐应用程序(或任何其他产生声音的应用程序,通常是互联网广播应用程序)中的音乐

I want to configure AVAudioSession that I can record video with audio and also play music from Music app (or any other app that is producing sound, typically internet radios apps)

我这样配置会话:

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation)

这是一种运行方式,当我的录制应用程序运行时,我可以开始播放音乐应用程序中的音频并录制视频(还录制音频).到目前为止很好.

This works the way that when my recording app is running I can start playing audio from music app and record video (also record audio as well). So far good.

现在,我将录制应用程序置于后台.音乐应用继续播放.没事.但是,当我的应用程序从背景音乐应用程序返回时,请停止其播放.我希望它继续.因此,在拍摄视频时,它基本上就像内置在相机应用程序中一样.有没有办法告诉系统我希望其他应用继续播放?

Now, I put my recording app to the background. Music app continues to play. It is fine. But, when my app is coming back from background music app stop its playback. I would like it to continue. So it basically behaves as build in camera app when shooting video. Is there a way to tell the system that I want other app to continue playback?

推荐答案

我几乎遇到了类似的问题,并且花了我一两天的时间才弄清问题所在.

I have almost met the similar problem and it is almost killed my one-two days for figure outing the problem.

在我的情况下,我使用AVCaptureSession进行视频/音频录制,它具有称为automaticallyConfiguresApplicationAudioSession的属性.根据Apple文档

In my case, I am used AVCaptureSession for video/audio recording and it has property called automaticallyConfiguresApplicationAudioSession. As per Apple docs

自动配置ApplicationAudioSession:一个布尔值,指示捕获会话是否自动更改 应用程序的共享音频会话中的设置.

automaticallyConfiguresApplicationAudioSession: A Boolean value that indicates whether the capture session automatically changes settings in the app’s shared audio session.

此属性的值默认为true,从而导致捕获 会话以自动配置应用程序的共享 AVAudioSession 最佳录制实例

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,如下所示,

So If we are manually setting the AVAudioSession, we should set this property as false like below,

captureSession.automaticallyConfiguresApplicationAudioSession = false;

将此属性设置为false并手动设置适当的AVAudioSession类别已解决了我的问题!希望它也对您有帮助!

Making this property to false and setting appropriate AVAudioSession categories manually has solved my issue! Hope it will help in your case too!

关于您的代码的更多观察结果

  • 不需要.notifyOthersOnDeactivation标志来激活音频 session此标志仅在停用音频会话时使用.那就是当您在setActive(_:options:)实例方法的beActive参数中传递false值时.因此,以下代码可以很好地激活会话 try? AVAudioSession.sharedInstance().setActive(true)
  • 默认情况下,AVAudioSessionCategoryPlayAndRecord类别的潜在客户 发出非常低的背景声音.如果要使音量正常,请按以下方式将选项.defaultToSpeaker.mixWithOthers一起使用

  • There is no need of .notifyOthersOnDeactivation flag to activating an audio session This flag is used only when deactivating your audio session. That is when you pass a value of false in the beActive parameter of the setActive(_:options:) instance method. So the below code fine for active a session try? AVAudioSession.sharedInstance().setActive(true)
  • In default the AVAudioSessionCategoryPlayAndRecord category leads to a very low volume of background sound. If you want normal volume, need to use the option .defaultToSpeaker with .mixWithOthers as follows,

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.mixWithOthers, .defaultToSpeaker])

好吧!那么我认为,如果您已正确完成所有其他操作,则现在可以正常工作.

Oke! then I think It will now work fine if you have done all other stuff properly.

注意** ,我创建了一个工作示例项目供您参考,您可以在

Note** I have created a working sample project for your reference and you can found it on GitHub Here

这篇关于AVAudioSession配置可与他人一起录制和播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆