我有一个需要的应用程序:
像Snapchat一样,camera-viewcontroller是" swipeview"因此总是在。
然而,当打开和关闭应用程序时,音乐会缩短"破解"噪音/声音会破坏音乐。
我在这里录制了它: https://soundcloud.com/morten-stulen/hacky-sound-ios (3次出现)
我使用这些设置更改了appdelegate中的AVAudiosession didFinishLaunchingWithOptions:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord,withOptions:
[AVAudioSessionCategoryOptions.MixWithOthers,
AVAudioSessionCategoryOptions.DefaultToSpeaker])
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("error")
}
我使用LLSimpleCamera控件进行视频录制,并将会话设置为:
_session.automaticallyConfiguresApplicationAudioSession = NO;
其他相机库似乎也有同样的问题: https://github.com/rFlex/SCRecorder/issues/127
https://github.com/rFlex/SCRecorder/issues/224
这家伙删除了audioDeviceInput,但我有点需要录制视频。 https://github.com/omergul123/LLSimpleCamera/issues/48
我也尝试使用Apple的代码" AvCam",我仍然遇到同样的问题。 Snapchat如何做到这一点?!
非常感谢任何帮助,我很乐意提供更多信息或代码!
答案 0 :(得分:5)
我做了类似于你想要的东西,但没有相机方面,但我认为这将做你想要的。我的应用程序允许与非全屏视频/音频混合的背景音频。当用户播放音频文件或全屏视频文件时,我会完全停止背景音频。
我执行 SoloAmbient 然后播放的原因是因为我允许在设备锁定时在后台播放音频。进入 SoloAmbient 将停止播放所有背景音乐,然后切换到播放可让我的音频在应用和后台播放。
这就是为什么你看到一个方法的调用,该方法在Unload方法中设置锁屏信息。在这种情况下,它将其清零,以便没有锁定屏幕信息。
在AppDelegate.swift中
//MARK: Audio Session Mixing
func allowBackgroundAudio()
{
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)
} catch {
NSLog("AVAudioSession SetCategory - Playback:MixWithOthers failed")
}
}
func preventBackgroundAudio()
{
do {
//Ask for Solo Ambient to prevent any background audio playing, then change to normal Playback so we can play while locked
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch {
NSLog("AVAudioSession SetCategory - SoloAmbient failed")
}
}
当我想要停止背景音频时,例如播放应该独立的音轨时,我会执行以下操作:
在MyAudioPlayer.swift中
func playUrl(url: NSURL?, backgroundImageUrl: NSURL?, title: String, subtitle: String)
{
ForgeHelper.appDelegate().preventBackgroundAudio()
if _mediaPlayer == nil {
self._mediaPlayer = MediaPlayer()
_mediaPlayer!.delegate = self
}
//... Code removed for brevity
}
当我完成媒体播放时,我会这样做:
private func unloadMediaPlayer()
{
if _mediaPlayer != nil {
_mediaPlayer!.unload()
self._mediaPlayer = nil
}
_controlView.updateForProgress(0, duration: 0, animate: false)
ForgeHelper.appDelegate().allowBackgroundAudio()
setLockScreenInfo()
}
希望这会帮助你!