ios – 使用Spotify /背景音乐打开相机

前端之家收集整理的这篇文章主要介绍了ios – 使用Spotify /背景音乐打开相机前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序需要有:

>使用应用程序时播放背景音乐(例如,识别)
>从AVPlayer观看电影时播放背景音乐
>录制视频时停止音乐

像Snapchat一样,相机视图控制器是“swipeview”的一部分,因此始终处于打开状态.

然而,当打开和关闭应用程序时,音乐会产生短暂的“破裂”噪音/声音,从而破坏音乐.

我记录在这里:
https://soundcloud.com/morten-stulen/hacky-sound-ios
(3次)

我使用这些设置来更改appAlegate didFinishLaunchingWithOptions中的AVAudiosession:

  1. do {
  2. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord,withOptions:
  3. [AVAudioSessionCategoryOptions.MixWithOthers,AVAudioSessionCategoryOptions.DefaultToSpeaker])
  4. try AVAudioSession.sharedInstance().setActive(true)
  5.  
  6. } catch {
  7. print("error")
  8. }

我使用LLSimpleCamera控件进行视频录制,我已经将会话设置为:

  1. _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

我也试过苹果的代码“AvCam”,我还是有同样的问题. Snapchat如何做到这一点?

任何帮助将不胜感激,我乐意提供更多的信息或代码

解决方法

我做的事情类似于你想要的,但没有相机方面,但我认为这将做你想要的.我的应用程序允许背景音频与非全屏视频/音频混合.当用户播放音频文件或全屏视频文件时,我完全停止背景音频.

我做SoloAmbient然后播放的原因是因为我允许我的音频在设备被锁定的背景中播放.去SoloAmbient将停止播放所有背景音乐,然后切换到播放可以让我的音频播放在应用程序以及后台.

这就是为什么你看到一个方法调用锁定屏幕信息在Unload方法.在这种情况下,它将其归零,以便没有锁屏信息.

在AppDelegate.swift中

  1. //MARK: Audio Session Mixing
  2. func allowBackgroundAudio()
  3. {
  4. do {
  5. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback,withOptions: .MixWithOthers)
  6. } catch {
  7. NSLog("AVAudioSession SetCategory - Playback:MixWithOthers Failed")
  8. }
  9. }
  10.  
  11. func preventBackgroundAudio()
  12. {
  13. do {
  14. //Ask for Solo Ambient to prevent any background audio playing,then change to normal Playback so we can play while locked
  15. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
  16. try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
  17. } catch {
  18. NSLog("AVAudioSession SetCategory - SoloAmbient Failed")
  19. }
  20. }

当我想要停止背景音频时,例如当播放应该是单独的音轨时,我执行以下操作:

在MyAudioPlayer.swift

  1. func playUrl(url: NSURL?,backgroundImageUrl: NSURL?,title: String,subtitle: String)
  2. {
  3. ForgeHelper.appDelegate().preventBackgroundAudio()
  4.  
  5. if _mediaPlayer == nil {
  6. self._mediaPlayer = MediaPlayer()
  7. _mediaPlayer!.delegate = self
  8. }
  9.  
  10. //... Code removed for brevity
  11. }

当我完成我的媒体播放,我这样做:

  1. private func unloadMediaPlayer()
  2. {
  3. if _mediaPlayer != nil {
  4. _mediaPlayer!.unload()
  5. self._mediaPlayer = nil
  6. }
  7.  
  8. _controlView.updateForProgress(0,duration: 0,animate: false)
  9.  
  10. ForgeHelper.appDelegate().allowBackgroundAudio()
  11. setLockScreenInfo()
  12. }

希望这可以帮助你!

猜你在找的iOS相关文章