Swift 5.1 错误:[plugin] AddInstanceForFactory:没有为 id <CFUUID 注册工厂

Ama*_*ala 29 swift swift5 swift5.1

应用程序崩溃并显示以下错误消息

2019-10-12 20:01:34.332334-0700 Awesome App[26368:3535170] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600002903280> F8BB1C28-BAE8-11D6-9C31-00039315CD46
Run Code Online (Sandbox Code Playgroud)

崩溃时的断点似乎与 AVAudioPlayer 有关

let path = Bundle.main.path(forResource: "menu_background.mp3", ofType:nil)!
audioPlayer = try AwesomeAudioPlayer(contentsOf: URL(fileURLWithPath: path)) ---> breakpoint

Run Code Online (Sandbox Code Playgroud)

Ran*_*man 15

I believe you all might have added the AVFoundation to the frameworks list in Project General Info tab.

Erroneous Code was as follows:

import SwiftUI
import AVFoundation

struct PlayerDetailView: View {
@State private var downloadedFilePath: URL = nil
var audioPlayer: AVAudioPlayer
 
var body: some View {
Run Code Online (Sandbox Code Playgroud)

在我将var audioPlayer: AVAudioPlayer声明移动到行之后,import AVFoundation它似乎正在工作。

因此,以下代码在一个SwiftUI项目中对我有用:

import SwiftUI
import AVFoundation
var audioPlayer: AVAudioPlayer!

struct PlayerDetailView: View {
    @State private var downloadedFilePath: URL = nil

    var body: some View {
        VStack {
            Button("Play the Downloaded Track") {
                if let downloadedPath = self.downloadedFilePath?.path, FileManager().fileExists(atPath: downloadedPath) {
                    do {
                        audioPlayer = try AVAudioPlayer(contentsOf: self.downloadedFilePath!)
                        guard let player = audioPlayer else { return }

                        player.prepareToPlay()
                        player.play()
                    } catch let error {
                        print(error.localizedDescription)
                    }
                } else {
                    print("The file doesn not exist at path || may not have been downloaded yet")
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我最初是在关注CodeWithChris 的这个教程,它的讨论也导致了上述变化。此外结帐下面的教程太多,如果你需要进一步的例子。

希望这对你们中的某个人有所帮助!

干杯!

  • 谢谢。我刚刚意识到,之前因为我将 `var player: AVAudioPlayer?;` 放在函数内导致了错误。当我像你一样将它移动到全局范围时。有效 (3认同)
  • 只有这适用于 SwiftUI。然而,视觉错误仍然出现。 (2认同)
  • 我尝试了很多不同的方法,这是唯一适用于 swiftui 的方法!谢谢。 (2认同)

小智 9

我认为这与模拟器设备能够对声音进行排队并播放它之前 AVAudioPlayer 超出范围有关……或者类似的事情。我是 Swift 的新手,对 iOS API 的经验为零。

以下是我在尝试放置后发现的结果:

var player: AVAudioPlayer!
Run Code Online (Sandbox Code Playgroud)

声音将播放或不播放,具体取决于上面线条的位置。

无论如何,我的模拟器设备上总会出现以下错误:

AddInstanceForFactory: No factory registered for id
Run Code Online (Sandbox Code Playgroud)

(我使用的是 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()     
    }
}
Run Code Online (Sandbox Code Playgroud)

发生错误并且播放声音

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()       
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我强烈建议使用 `var player: AVAudioPlayer?` 而不是 `var player: AVAudioPlayer!` (2认同)

小智 5

我相信错误消息是对模拟器的警告,因此它并不重要。

我认为您的问题是代码中的错误。应该是这样的:

let path = Bundle.main.path(forResource: "menu_background", ofType:"mp3") audioPlayer = try AwesomeAudioPlayer(contentsOf: URL(fileURLWithPath: path!)) ---> breakpoint

其中forResource是文件名,ofType是扩展名。你也可以使用Bundle.main.url,它看起来像这样:

let path = Bundle.main.url(forResource: "menu_background", withExtension:"mp3") audioPlayer = try AwesomeAudioPlayer(contentsOf: URL(fileURLWithPath: path!)) ---> breakpoint

  • 刚刚在我的手机上运行它,没有任何消息,所以你对模拟器的看法是对的 (6认同)

小智 1

我在另一个有关 AVAudioPlayer 的 stackoverflow 线程中找到了解决方案,如下:

如果你初始化你的AVAudioPlayer喜欢

var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer()或者wrongMusicPlayer = AVAudioPlayer()以任何方法,请删除它并像这样声明var wrongMusicPlayer: AVAudioPlayer!。

  • 不幸的是,这不是解决方案。 (43认同)
  • @alionthego 虽然我还没有深入研究这个错误,但我发现这是使用模拟器运行时遇到的错误。设备上没有收到错误消息。 (9认同)
  • 有人找到解决方案了吗?遇到同样的问题,这个接受的答案对我不起作用 (8认同)