From 3e312ae68f5c5bff7abe7388ec515abb6650c71b Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 17:57:29 -0500 Subject: [PATCH 1/6] 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/6] 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/6] 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 { From 5cb87cddf58a75db99d15c513115713b868ef112 Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 18:32:57 -0500 Subject: [PATCH 4/6] Allow Bluetooth in this session #147 Allow bluetooth in this session because it is ultimately the audio session Cookbook uses when the app is first loaded. Thus, users wearing a Bluetooth-enabled device will be able to listen to audio through said device. --- .../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 5f12c91..673c3d6 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -105,7 +105,7 @@ class MultiSegmentPlayerConductor: ObservableObject, HasAudioEngine { func setAudioSessionCategoriesWithOptions() { do { - try AudioKit.Settings.session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .mixWithOthers]) + try AudioKit.Settings.session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .mixWithOthers, .allowBluetooth, .allowBluetoothA2DP]) try AudioKit.Settings.session.setActive(true) } catch { assertionFailure(error.localizedDescription) From d9de693ec5bbd0c1e90139e40ae0a85bb80f65d8 Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 18:36:27 -0500 Subject: [PATCH 5/6] hound --- .../Recipes/AudioPlayer/MultiSegmentPlayerView.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift index 673c3d6..41f35fc 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -105,7 +105,11 @@ class MultiSegmentPlayerConductor: ObservableObject, HasAudioEngine { func setAudioSessionCategoriesWithOptions() { do { - try AudioKit.Settings.session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .mixWithOthers, .allowBluetooth, .allowBluetoothA2DP]) + try AudioKit.Settings.session.setCategory(.playAndRecord, + options: [.defaultToSpeaker, + .mixWithOthers, + .allowBluetooth, + .allowBluetoothA2DP]) try AudioKit.Settings.session.setActive(true) } catch { assertionFailure(error.localizedDescription) From d42c183e5bd41ac79244f00b5e979e9bd322ecfa Mon Sep 17 00:00:00 2001 From: Evan Murray Date: Mon, 8 Jan 2024 18:37:07 -0500 Subject: [PATCH 6/6] hound --- .../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 41f35fc..ccc6183 100644 --- a/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift +++ b/Cookbook/CookbookCommon/Sources/CookbookCommon/Recipes/AudioPlayer/MultiSegmentPlayerView.swift @@ -105,7 +105,7 @@ class MultiSegmentPlayerConductor: ObservableObject, HasAudioEngine { func setAudioSessionCategoriesWithOptions() { do { - try AudioKit.Settings.session.setCategory(.playAndRecord, + try AudioKit.Settings.session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .mixWithOthers, .allowBluetooth,