Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to update the UI with a Timer, use ProgressView with a Date Interval #156

Merged
merged 4 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CompletionHandlerConductor: ObservableObject, HasAudioEngine {
let engine = AudioEngine()
var player = AudioPlayer()
var fileURL = [URL]()
var currentTime = 0.0
@Published var playDuration = 0.0
var currentFileIndex = 0

// Load the files to play
Expand All @@ -22,22 +22,17 @@ class CompletionHandlerConductor: ObservableObject, HasAudioEngine {
}
fileURL.append(url)
}
try? player.load(url: fileURL[0])
}

/* Completion handler function:
a function returning void */
func playNextFile() {
currentTime = 0.0
if currentFileIndex < 4 {
currentFileIndex += 1
try? player.load(url: fileURL[currentFileIndex])
player.play()
} else {
currentFileIndex = 0
try? player.load(url: fileURL[currentFileIndex])
player.play()
}
startPlaying()
}

init() {
Expand All @@ -49,50 +44,38 @@ class CompletionHandlerConductor: ObservableObject, HasAudioEngine {
player.completionHandler = playNextFile
}

// Player functions
func loadFile(url: URL) {
do {
try player.load(url: url)
} catch {
Log(error.localizedDescription, type: .error)
func startPlaying() {
try? player.load(url: fileURL[currentFileIndex])
player.play()
if let duration = player.file?.duration {
playDuration = duration
}
}

}

struct AudioPlayerCompletionHandler: View {
let timer = Timer.publish(every: 0.01, on: .main, in: .common).autoconnect()
@State var currentPlayTime = 0.0
@State var playDuration = 0.0
@State var playLabel = ""
@StateObject var conductor = CompletionHandlerConductor()

var body: some View {
Text("AudioPlayer Completion Handler")
.padding()
Text("This will play one file. Once it completes, it will play another!")
Text("That's one thing a completion handler can do!")
Text("This will play one file. Once it completes, it will play another.")
Text("That's one thing a completion handler can do.")
VStack {
ProgressView(playLabel, value: currentPlayTime, total: playDuration)
let playLabel = "Playing: " + conductor.fileURL[conductor.currentFileIndex]
.deletingPathExtension().lastPathComponent
let playTimeRange = Date()...Date().addingTimeInterval(conductor.playDuration)
ProgressView(timerInterval: playTimeRange, countsDown: false) {
Text(playLabel)
}
}
.onAppear {
conductor.start()
conductor.player.play()
conductor.startPlaying()
}
.onDisappear {
conductor.stop()
}
.onReceive(timer) { _ in
currentPlayTime = conductor.currentTime
if let duration = conductor.player.file?.duration {
playDuration = duration
}
if currentPlayTime < playDuration {
conductor.currentTime += 0.01
} else {
currentPlayTime = 0.0
conductor.currentTime = 0.0
}
playLabel = "Playing: " + conductor.fileURL[conductor.currentFileIndex]
.deletingPathExtension().lastPathComponent
}
}
}
Loading