【问题标题】:How to play sounds in iOS 10 when app is in the background with Swift 3当应用程序在后台使用 Swift 3 时如何在 iOS 10 中播放声音
【发布时间】:2016-11-17 03:21:04
【问题描述】:

我正在开发一个锻炼应用程序,该应用程序需要能够在应用程序进入后台以及屏幕锁定时播放声音。我还需要允许播放来自其他应用的声音(例如音乐),同时还允许播放我的音效。

我在 iOS 9 中使用 AVAudioSessionCategoryAmbient 进行了这项工作,但是一旦我的应用程序变为非活动状态或屏幕锁定,声音就会停止。

这是我在演示应用中的代码的简化版本:

import UIKit
import AVFoundation

class ViewController: UIViewController {

var session: AVAudioSession = AVAudioSession.sharedInstance()
var timer = Timer()
let countdownSoundFile = "154953__keykrusher__microwave-beep"
let countdownSoundExt = "wav"
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {
    super.viewDidLoad()

    activateAudio()

}

func activateAudio() {
        _ = try? session.setCategory(AVAudioSessionCategoryAmbient, with: [])
        _ = try? session.setActive(true, with: [])
}

@IBAction func play() {
    timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(ViewController.playSound), userInfo: nil, repeats: true)
}

func playSound() {

    if let soundURL = Bundle.main.url(forResource: countdownSoundFile, withExtension: countdownSoundExt) {
        do {

            print("sound playing")
            try audioPlayer = AVAudioPlayer(contentsOf: soundURL)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
        } catch {
            print("no sound found")
        }
    }

}
}

我还检查了后台模式中的Audio, Airplay, and Picture in Picture 功能,它在我的 plist 中添加了必需的后台模式。

我正在设备上对此进行测试,一旦我锁定屏幕或按下主页按钮,我的声音就会停止,一旦我的应用再次激活,声音就会恢复。

我找到了解决方法:

var mySound: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
AudioServicesPlaySystemSound(mySound);

但是这不允许改变声音的音量,而且我还需要使用 AVSpeechSynthesizer,这个解决方法不起作用。

关于我可以做些什么来完成这项工作的任何想法?

编辑:

我将类别行更改为_ = try? session.setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers]),这允许在应用程序运行时播放音乐,但是当屏幕锁定或进入后台时声音仍然停止。我需要声音在这些场景中继续播放。

编辑:

正如答案所指出的,当我的应用程序处于后台时,我无法播放声音,但使用 _ = try? session.setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers]) 确实允许在屏幕关闭并与来自其他应用程序的声音(音乐)混合时播放声音。

【问题讨论】:

    标签: ios swift swift3 avaudioplayer ios10


    【解决方案1】:

    您无法使用环境音频会话类别在后台播放。您的类别必须是播放。

    要允许播放来自其他应用的声音,请使用可混合选项 (.mixWithOthers) 修改播放类别。

    (另外请记住,您可以随时更改您的类别。您可以在大多数时间将其设置为 Ambient,但当您知道您将进入后台时切换到 Playback 以便您可以继续播放.)

    编辑我还想到,还有另一个可能需要澄清的误解。在后台您不能(轻松)开始新的声音。当您的应用进入后台时,背景音频允许您做的就是继续播放当前声音。一旦声音停止(在后台),您的应用就会暂停,这就是结束。

    【讨论】:

    • 并且还需要为音频启用Background Modes Capabilities
    • @iphonic 请阅读问题。他说他做到了。问题是为什么这还不够。
    • @matt 我用更新编辑了我的问题:您的建议允许在我的应用程序播放声音时播放来自不同应用程序的音乐,但是一旦屏幕关闭或应用程序进入后台,我的应用程序声音停止。有什么想法吗?
    • 让我们调试一下。如果您(暂时)放弃对可混合性的想法并且使用可混合选项,会发生什么情况。您只需使用 Playback,简单明了。那么你的声音会在背景中继续吗?
    • 另外,请查看我对答案的新编辑,因为我有点担心您可能对背景声音的含义有错误的认识。
    【解决方案2】:

    在这里查看this answer 的详细信息。您还需要从应用程序设置中启用音频模式。

    do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
    print("AVAudioSession Category Playback OK")
    do {
        try AVAudioSession.sharedInstance().setActive(true)
        print("AVAudioSession is Active")
    } catch {
        print(error)
    }
    } catch {
    print(error)
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2021-10-11
      • 2014-06-11
      • 2020-10-03
      相关资源
      最近更新 更多