为什么当计时器达到 0 秒且未按照指示播放声音 Doorbell-2.mp3 时,我会收到错误代码(( AddInstanceForFactory:没有为 id 注册工厂
我将感谢所有的帮助
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()
}
}
我已经尝试检查文件名及其存在性......仍然没有运气
audioPlayer 应在 TimerView 的最高级别声明,作为 SwiftUI 的 @State:
@State var audioPlayer: AVAudioPlayer?
然后更改 do 子句中的这些行:
let audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
audioPlayer.play()
为此要使用audioPlayer:
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
audioPlayer?.play()