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

11

53

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

Whoever answered 13/10, 2019 at 5:10 Comment(1)
Sound is not working when you're iPhone is in silent while in a debugging stateAmidships
O
1

I have found the solution in another stackoverflow thread about AVAudioPlayer, here it is :

If you initialize your AVAudioPlayer like

var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer() OR wrongMusicPlayer = AVAudioPlayer() in any method then please remove it and just Declare like var wrongMusicPlayer: AVAudioPlayer!.

Overline answered 15/10, 2019 at 10:21 Comment(3)
NOT the solution, unfortunately.Mechanotherapy
has anybody found a solution to this yet? having the same problem and this accepted answer doesn't work for meSeedbed
@Seedbed Although I haven't dived deep into this error yet, what I've found is that it's an error you get when you run using simulator. Didn't get the error on device.Generalist
Q
24

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.

Quadrat answered 9/4, 2020 at 22:37 Comment(6)
Only this worked for SwiftUI. However, the visual error still appears.Burcham
@Oleksandr what's the Visual error? I didn't get you?Quadrat
[plugin] AddInstanceForFactory: No factory registered for idBurcham
I tried so many different ways, this is the only way works for swiftui!! Thank you.Arlettaarlette
@Oleksandr I will have a look and get back to you within the weekend prolly! :-)Quadrat
Thanks. I just realize, before because I place the var player: AVAudioPlayer?; inside the function caused to the Error. When I move it to the global scope like you. It workedSeigel
C
10

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

Colitis answered 3/12, 2019 at 8:10 Comment(1)
Just ran it on my phone and there was no message so you're right about the simulator👍Phlegethon
S
10

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()       
  }
}
Semmes answered 14/9, 2020 at 19:6 Comment(1)
I would strongly advice to use var player: AVAudioPlayer? instead of var player: AVAudioPlayer!Staffer
F
2

In my case totally different...

The issue is solved after doing... var player = AVAudioPlayer()

It didn't work in... var player:AVAudioPlayer!

(Xcode 12.2)

Foveola answered 28/1, 2021 at 11:10 Comment(1)
var player:AVAudioPlayer! works fine if you initialize its value at the proper place, like in viewDidLoad for example, or even in your method just before it is used.Eclat
M
2

I think I have solved the issue (hard to prove a negative, since the error was sporadic). I have my AVAudioPlayer in a separate class:

import SwiftUI
import AVFoundation
var audioPlayer: AVAudioPlayer?

class AudioManager {
    
    var userSettings = UserSettings()
    func playSoundEffect(_ assetName:String) {
        
        if !userSettings.soundDisabled {
            guard let audioData = NSDataAsset(name: assetName)?.data else {
                fatalError("Unable to find asset \(assetName)")
            }
            
            do {
                audioPlayer = try AVAudioPlayer(data: audioData)
                audioPlayer?.prepareToPlay()
                audioPlayer?.play()
            } catch {
                print(error.localizedDescription)
            }
        }
//        audioPlayer?.stop()
    }
    func stopPlay() {
        audioPlayer?.setVolume(0.0, fadeDuration: 0.25)
    }
    
}

Then, in the SwiftUI views where I need to play audio, be certain to import AVFoundation.

import SwiftUI
import AVFoundation

struct MyView: View {
    ...
    let audioManager = AudioManager()
    ...
}
Misprint answered 11/1, 2022 at 17:55 Comment(0)
O
1

I have found the solution in another stackoverflow thread about AVAudioPlayer, here it is :

If you initialize your AVAudioPlayer like

var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer() OR wrongMusicPlayer = AVAudioPlayer() in any method then please remove it and just Declare like var wrongMusicPlayer: AVAudioPlayer!.

Overline answered 15/10, 2019 at 10:21 Comment(3)
NOT the solution, unfortunately.Mechanotherapy
has anybody found a solution to this yet? having the same problem and this accepted answer doesn't work for meSeedbed
@Seedbed Although I haven't dived deep into this error yet, what I've found is that it's an error you get when you run using simulator. Didn't get the error on device.Generalist
V
1

You could use do/catch to avoid the crash and examine the exception, or ignore the issue all together with try?. For me, this was only showing up in the simulator when calling:

try? AVAudioSession.sharedInstance().setCategory(.playback)

I think it's safe to ignore it in my case.

Varhol answered 5/1, 2020 at 3:56 Comment(0)
L
1

Similar problem - using Preferences > Sound > Output different than Logictech USB Headset led to app that executed perfect & played sound w/o problems. Was never a problem outside of the Simulator - code on a device worked fine.

TL;DR This is an especially gnarly & obtuse problem. I also encountered an issue with an unexpected: No factory registered for id

After a short wait, it was also followed by several other console-reported errors, including: HALC_ProxyIOContext::IOWorkLoop: the server failed to start, Error: AQMEIO.cpp:182:AwaitIOCycle: timed out after 15.000s CA_UISoundClient.cpp:244:StartPlaying_block_invoke: CA_UISoundClientBase::StartPlaying: AddRunningClient failed

error when trying to play sound. Xcode 12.2, Mac OSX Catalina 10.15.7, simulator running iOS 14.2. Code had previously worked on prior versions of the simulator. Always had proper import of AVFoundation & declaration of AVAudioPlayer class property as: var audioPlayer: AVAudioPlayer!

In my case it seems to be related to audio drivers in Mac OSX. This problem ONLY happened when I had Mac System Preferences > Sound > Output set to my Logitech USB Headset. The code otherwise worked when: played through my LG Monitor, played through my AirPods Pro, and when executing outside the simulator and on a device > my iPhone 11 Pro.

Spent over an hour trying to diagnose the issue before restarting & noticing audio working when headset wasn't used for output. Switching the Preferences > Sound > Output settings to something other than the Logitech USB headset immediately fixed the problem in all other playback instances.

Not even sure where to file this issue as an Apple bug, but hoping it helps someone. Am assuming it's an OS-specific issue & not one that'll result in a problem w/the app or code.

Likeness answered 21/11, 2020 at 16:37 Comment(0)
T
0

I found a solution in Here2Huynh's answer.

let path = Bundle.main.path(forResource: "menu_background.mp3", ofType:nil)! 

here change

ofType:nil

to

ofType: "mp3"

and then again.There is no error message when using the emulator.

By the way, I am using the swiftui project

Tannie answered 28/3, 2021 at 11:7 Comment(0)
R
0

Simulator -> I/O -> Keyboard -> uncheck all enter image description here

Rene answered 6/8, 2022 at 16:24 Comment(0)
M
0

It's a warning related to the simulator indeed. To silence it (and play sound only on real devices):

#if !targetEnvironment(simulator)
    // play sound here
#endif
Mclane answered 14/7, 2023 at 17:57 Comment(1)
It happens on real devices too.Scibert

© 2022 - 2024 — McMap. All rights reserved.