From 3e312ae68f5c5bff7abe7388ec515abb6650c71b Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 17:57:29 -0500 Subject: [PATCH 1/3] Make conductor state object #145 So a new audio engine is created each time the view is loaded and doesn't contain the stopped state of the previous audio engine --- .../Recipes/AudioPlayer/MultiSegmentPlayerView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift index a506658..04ce0ca 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -126,7 +126,7 @@ class MultiSegmentPlayerConductor: ObservableObject { } struct MultiSegmentPlayerView: View { - @ObservedObject var conductor = MultiSegmentPlayerConductor() + @StateObject var conductor = MultiSegmentPlayerConductor() var currentTimeText: String { let currentTime = String(format: "%.1f", conductor.timeStamp) From fcd9db4f610de12c9879288d23c7dd877e4d160d Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 18:01:26 -0500 Subject: [PATCH 2/3] Inherit from HasAudioEngine protocol So the view's engine can have start/stop functionality. --- .../Recipes/AudioPlayer/MultiSegmentPlayerView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift index 04ce0ca..299a61b 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -3,7 +3,7 @@ import AudioKitEX import AudioKitUI import SwiftUI -class MultiSegmentPlayerConductor: ObservableObject { +class MultiSegmentPlayerConductor: ObservableObject, HasAudioEngine { let engine = AudioEngine() let player = MultiSegmentAudioPlayer() From 0f20fee95cfec2fbac50c2cde706bc9dfc1c5815 Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 18:05:13 -0500 Subject: [PATCH 3/3] Add start/stop functionality This way audio will stop playing when the view is closed and the engine can start when the view is loaded --- .../Recipes/AudioPlayer/MultiSegmentPlayerView.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift index 299a61b..5f12c91 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -164,6 +164,12 @@ struct MultiSegmentPlayerView: View { Spacer() } .navigationBarTitle(Text("Multi Segment Player")) + .onAppear { + conductor.start() + } + .onDisappear { + conductor.stop() + } } struct PlayPauseView: View {