From ab7d34337ff4320290fcc38d0dea4a25a1dc777f Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 12 Sep 2022 17:22:50 +0800 Subject: [PATCH] Improve voice changing during playback --- .../Sources/App/Views/AudioPlayer/MiniPlayer.swift | 1 + .../Services/AudioSession/AudioController.swift | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift index 3a639aa6e..4011763c5 100644 --- a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift @@ -353,6 +353,7 @@ public struct MiniPlayer: View { ForEach(audioController.voiceList ?? [], id: \.key.self) { voice in Button(action: { audioController.currentVoice = voice.key + self.showVoiceSheet = false }) { HStack { Text(voice.name) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 0fb438ad0..efb755440 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -44,9 +44,9 @@ let VOICES = [ VoicePair(firstKey: "en-US-CoraNeural", secondKey: "en-US-ChristopherNeural", firstName: "Cora (USA)", secondName: "Christopher (USA)"), VoicePair(firstKey: "en-US-ElizabethNeural", secondKey: "en-US-EricNeural", firstName: "Elizabeth (USA)", secondName: "Eric (USA)"), VoicePair(firstKey: "en-CA-ClaraNeural", secondKey: "en-CA-LiamNeural", firstName: "Clara (Canada)", secondName: "Liam (Canada)"), - VoicePair(firstKey: "en-UK-LibbyNeural", secondKey: "en-UK-EthanNeural", firstName: "Libby (UK)", secondName: "Ethan (UK)"), + VoicePair(firstKey: "en-GB-LibbyNeural", secondKey: "en-GB-EthanNeural", firstName: "Libby (UK)", secondName: "Ethan (UK)"), VoicePair(firstKey: "en-AU-NatashaNeural", secondKey: "en-AU-WilliamNeural", firstName: "Natasha (Australia)", secondName: "William (Australia)"), - VoicePair(firstKey: "en-ID-NeerjaNeural", secondKey: "en-ID-PrabhatNeural", firstName: "Neerja (India)", secondName: "Prabhat (India)"), + VoicePair(firstKey: "en-IN-NeerjaNeural", secondKey: "en-IN-PrabhatNeural", firstName: "Neerja (India)", secondName: "Prabhat (India)"), VoicePair(firstKey: "en-SG-LunaNeural", secondKey: "en-SG-WayneNeural", firstName: "Luna (Singapore)", secondName: "Wayne (Singapore)") ] @@ -223,7 +223,12 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate } private func downloadAndPlayFrom(_ currentIdx: Int, _ currentOffset: Double) { + let desiredState = state + + pause() playbackTask?.cancel() + document = nil + synthesizer = nil if let pageId = item?.id { Task { @@ -232,6 +237,8 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate let synthesizer = SpeechSynthesizer(appEnvironment: self.appEnvironment, networker: self.networker, document: self.document!) self.durations = synthesizer.estimatedDurations(forSpeed: self.playbackRate) self.synthesizer = synthesizer + + self.state = desiredState self.synthesizeFrom(start: currentIdx, playWhenReady: self.state == .playing, atOffset: currentOffset) } } @@ -258,7 +265,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate } public func isLoadingItem(item: LinkedItem) -> Bool { - (state == .loading || player?.currentItem?.status == .unknown) && self.item == item + (state == .loading || player?.currentItem == nil || player?.currentItem?.status == .unknown) && self.item == item } public func isPlayingItem(item: LinkedItem) -> Bool {