我认为这与 AVAudioPlayer 在模拟器设备能够排队声音并播放它之前超出范围有关......或类似的东西。我是 Swift 的新手,我对 iOS API 的经验为零。
这是我在尝试放置以下内容后发现的:
var player: AVAudioPlayer!
声音将播放或不播放,具体取决于上面行的位置。
无论如何,我的模拟器设备上总是会出现以下错误:
AddInstanceForFactory: No factory registered for id
(我在 2013 年末的 MacBook Pro + MacOS Catalina + Xcode 11.7 上,我在运行 iOS 13.7 的 iPhone SE 2 模拟器上对此进行了测试)
尽管错误不断发生,但我很高兴我至少可以在模拟器上播放声音......
发生错误并且声音不播放...
import UIKit
import AVFoundation
class ViewController: UIViewController {
func playTheSound() {
// initializing the "player" variable within the "playTheSound()" function will cause the "AddInstanceForFactory: No factory registered for id" error and the sound WILL NOT play on the simulator
var player: AVAudioPlayer!
let url = Bundle.main.url(forResource: "funny_sound", withExtension: "mp3")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
}
}
发生错误并播放声音
import UIKit
import AVFoundation
class ViewController: UIViewController {
// initializing the "player" variable at the class level will still cause the "AddInstanceForFactory: No factory registered for id" error to happen, but the sound will still play on the simulator
var player: AVAudioPlayer!
func playTheSound() {
let url = Bundle.main.url(forResource: "funny_sound", withExtension: "mp3")
player = try! AVAudioPlayer(contentsOf: url!)
player.play()
}
}