gpt4 book ai didi

ios - 当应用程序在后台使用 Swift 3 时如何在 iOS 10 中播放声音

转载 作者:可可西里 更新时间:2023-11-01 00:02:50 25 4
gpt4 key购买 nike

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

我通过使用 AVAudioSessionCategoryAmbient 在 iOS 9 中实现了此功能,但是一旦我的应用程序变为非事件状态或屏幕锁定,声音就会停止。

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

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")
}
}

}
}

我还检查了背景模式中的音频、Airplay 和画中画 功能,这将所需的背景模式添加到我的 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]) 确实允许在屏幕关闭时播放声音并与来自其他应用程序的声音(音乐)混合。

最佳答案

您不能使用环境 Audio Session 类别在后台播放。您的类别必须是播放。

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

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

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

关于ios - 当应用程序在后台使用 Swift 3 时如何在 iOS 10 中播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com