a fix for (( AddInstanceForFactory: No factory registered for id <CFUUID 0x6000002b04c0> F8BB1C28-BAE8-11D6-9C31-00039315CD46 ))

33 Views Asked by At

why do i get an error code (( AddInstanceForFactory: No factory registered for id <CFUUID 0x6000002b04c0> F8BB1C28-BAE8-11D6-9C31-00039315CD46 )) when the timer reaches 0 sec and the does not play sound doorbell-2.mp3 as instructed.

i will appreciate all the help

import SwiftUI
import AVFoundation



struct TimerView: View {
    @State private var countdownSeconds1 = 20
    @State private var countdownSeconds2 = 180
    @State private var countdownSeconds3 = 300
    @State private var countdownSeconds4 = 420
    @State private var timer1: Timer?
    @State private var timer2: Timer?
    @State private var timer3: Timer?
    @State private var timer4: Timer?
    
    @State private var stopwatchSeconds = 0
    @State private var stopwatchTimer: Timer?
    
    
    

    var body: some View {
        List {
            Section(header: Text("Timers")) {
                TimerRow(label: "20 sec", countdownSeconds: $countdownSeconds1, startTimer: startTimer1)
                TimerRow(label: "3 Min", countdownSeconds: $countdownSeconds2, startTimer: startTimer2)
                TimerRow(label: "5 Min", countdownSeconds: $countdownSeconds3, startTimer: startTimer3)
                TimerRow(label: "7 Min", countdownSeconds: $countdownSeconds4, startTimer: startTimer4)
            }

            Section(header: Text("Stopwatch")) {
                HStack {
                    Text(formatTime(stopwatchSeconds))
                        .bold()
                        .font(.title)
                    Spacer()
                    Button(action: {
                        startStopwatch()
                    }) {
                        Text("Start")
                            .bold()
                            .font(.title)
                            .foregroundColor(.green)
                    }
                }
            }

            HStack {
                Spacer()
                Button(action: {
                    startAllTimers()
                }) {
                    Text("Start All Timers")
                        .bold()
                        .font(.title2)
                        .foregroundColor(.green)
                }
                Spacer()
            }
        }
        .navigationBarTitle("Timer")
    }

    func startTimer1() {
        resetTimer(timer: &timer1)
        timer1 = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            if countdownSeconds1 > 0 {
                countdownSeconds1 -= 1
                if countdownSeconds1 == 0 {
                    handleTimerEvent(soundName: "doorbell-2") // Replace "your_sound_file1" with your actual sound file name
                }
            }
        }
    }

    func startTimer2() {
        resetTimer(timer: &timer2)
        timer2 = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            if countdownSeconds2 > 0 {
                countdownSeconds2 -= 1
                if countdownSeconds2 == 0 {
                    handleTimerEvent(soundName: "your_sound_file2") // Replace "your_sound_file2" with your actual sound file name
                }
            }
        }
    }

    func startTimer3() {
        resetTimer(timer: &timer3)
        timer3 = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            if countdownSeconds3 > 0 {
                countdownSeconds3 -= 1
                if countdownSeconds3 == 0 {
                    handleTimerEvent(soundName: "your_sound_file3") // Replace "your_sound_file3" with your actual sound file name
                }
            }
        }
    }

    func startTimer4() {
        resetTimer(timer: &timer4)
        timer4 = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            if countdownSeconds4 > 0 {
                countdownSeconds4 -= 1
                if countdownSeconds4 == 0 {
                    handleTimerEvent(soundName: "your_sound_file4") // Replace "your_sound_file4" with your actual sound file name
                }
            }
        }
    }

    func resetTimer(timer: inout Timer?) {
        timer?.invalidate()
        timer = nil
    }

    func handleTimerEvent(soundName: String) {
        if let soundURL = Bundle.main.url(forResource: soundName, withExtension: "mp3") {
            do {
                let audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
                           audioPlayer.play()
            } catch {
                print("Error playing sound: \(error.localizedDescription)")
            }
        } else {
           // print("Sound file not found: \(soundName)")
            if let bundlePath = Bundle.main.path(forResource: soundName, ofType: "mp3", inDirectory: "MediaFiles") {
                print("File found at path: \(bundlePath)")
            } else {
                print("File not found.")
            }

        }
        // You can add additional actions here when the timer reaches zero
    }



    func startAllTimers() {
        startTimer1()
        startTimer2()
        startTimer3()
        startTimer4()
        startStopwatch()
    }
    
    func startStopwatch() {
        resetStopwatch()
        stopwatchTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            stopwatchSeconds += 1
        }
    }

    func resetStopwatch() {
        stopwatchTimer?.invalidate()
        stopwatchSeconds = 0
    }

    func formatTime(_ seconds: Int) -> String {
        let minutes = seconds / 60
        let seconds = seconds % 60
        return String(format: "%02d:%02d", minutes, seconds)
    }
}

struct TimerRow: View {
    var label: String
    @Binding var countdownSeconds: Int
    var startTimer: () -> Void

    var body: some View {
        HStack {
            VStack {
                HStack{
                    Text(formatTime(countdownSeconds))
                        .bold()
                        .font(.title)
                    Spacer()
                }
                HStack {
                    Text(label)
                    Spacer()
                }
            }
            Spacer()
            Button(action: {
                startTimer()
            }) {
                Text("Start")
                    .bold()
                    .font(.title)
                    .foregroundColor(.green)
            }
        }
    }

    func formatTime(_ seconds: Int) -> String {
        let minutes = seconds / 60
        let seconds = seconds % 60
        return String(format: "%02d:%02d", minutes, seconds)
    }
}

struct TimerView_Previews: PreviewProvider {
    static var previews: some View {
        TimerView()
    }
}

i have tried checking the file name and its existence ... still no luck

1

There are 1 best solutions below

0
Marcy On

The audioPlayer should be declared at the highest level of TimerView, as a @State for SwiftUI:

@State var audioPlayer: AVAudioPlayer?

Then change these lines in the do clause:

let audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
audioPlayer.play()

To this to use audioPlayer:

audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
audioPlayer?.play()

Note that the message still occurs when using AVFoundation with the simulator but not with an iPhone or iPad. In other words, you can ignore the message and eventually Apple may change AVFoundation to stop the message from occurring.