Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 5.1 Error: [plugin] AddInstanceForFactory: No factory registered for id <CFUUID

App crashes with the following error message

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

The breakpoint at crash seems to be related to AVAudioPlayer

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

like image 595
Amar Kanala Avatar asked Oct 13 '19 05:10

Amar Kanala


3 Answers

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 {

And after I moved the var audioPlayer: AVAudioPlayer declaration to just after the line of import AVFoundation line it seemed to be working.

So following code worked for me in a SwiftUI project:

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

I was initially following this tutorial of CodeWithChris and its discussion also led to above change. Also checkout following tutorial too if you need further examples.

Hope this will be helpful to someone of you out there!

Cheers!

like image 82
Randika Vishman Avatar answered Nov 06 '22 23:11

Randika Vishman


I believe the error message is a warning for simulators hence it is not important.

I think your issue is a bug in your code. Should be something like this:

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

Where the forResource is the name of the file and ofType is the extension. You can also use Bundle.main.url which will look like this:

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

like image 31
Here2Huynh Avatar answered Nov 07 '22 01:11

Here2Huynh


I think it has to do with the AVAudioPlayer going out of scope before the simulator device is able to queue up the sound and play it...or something along those lines. I'm brand new to Swift and I have zero experience with iOS APIs.

Here's what I discovered after experimenting with the placement of:

var player: AVAudioPlayer!

The sound will either PLAY or NOT PLAY depending on the placement of the line above.

Regardless, the following error will always occur on my Simulator devices:

AddInstanceForFactory: No factory registered for id

(I'm on a Late 2013 MacBook Pro + MacOS Catalina + Xcode 11.7 and I tested this on an iPhone SE 2 simulator running iOS 13.7)

Although the error keeps occurring, I'm happy that I at least got the sound to play on the Simulator...

Error occurs and sound DOES NOT play...

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

Error occurs and sound DOES 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()       
  }
}
like image 4
seanbuild Avatar answered Nov 06 '22 23:11

seanbuild