问题
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
回答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
!.
回答2:
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!
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/58360765/swift-5-1-error-plugin-addinstanceforfactory-no-factory-registered-for-id-c