From 8db3b397d7dbc8cfb2dd8786d9581648a0aa8b2d Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Fri, 9 Sep 2022 16:34:42 +0800 Subject: [PATCH] UI for changing playback rate --- .../App/Views/AudioPlayer/MiniPlayer.swift | 28 +++++++++++++++---- .../AudioSession/AudioController.swift | 7 +++++ .../Sources/Utils/UserDefaultKeys.swift | 1 + 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift index ad60f432d..d5a053e58 100644 --- a/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/AudioPlayer/MiniPlayer.swift @@ -248,14 +248,14 @@ public struct MiniPlayer: View { HStack { Menu { - Button("1.0×", action: {}) - Button("1.2×", action: {}) - Button("1.5×", action: {}) - Button("1.7×", action: {}) - Button("2.0×", action: {}) + playbackRateButton(rate: 1.0, title: "1.0×", selected: audioController.playbackRate == 1.0) + playbackRateButton(rate: 1.2, title: "1.2×", selected: audioController.playbackRate == 1.2) + playbackRateButton(rate: 1.5, title: "1.5×", selected: audioController.playbackRate == 1.5) + playbackRateButton(rate: 1.7, title: "1.7×", selected: audioController.playbackRate == 1.7) + playbackRateButton(rate: 2.0, title: "2.0×", selected: audioController.playbackRate == 2.0) } label: { VStack { - Text("1.0×") + Text(String(format: "%.1f×", audioController.playbackRate)) .font(.appCallout) .lineLimit(0) } @@ -312,6 +312,22 @@ public struct MiniPlayer: View { } } + func playbackRateButton(rate: Double, title: String, selected: Bool) -> some View { + Button(action: { + audioController.playbackRate = rate + }) { + HStack { + Text(title) + Spacer() + if selected { + Image(systemName: "checkmark") + } + } + .contentShape(Rectangle()) + } + .buttonStyle(PlainButtonStyle()) + } + public var body: some View { ZStack(alignment: .center) { presentingView diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 3cc84f358..ae6aa03d0 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -10,6 +10,7 @@ import CryptoKit import Foundation import MediaPlayer import Models +import SwiftUI import Utils public enum AudioControllerState { @@ -235,6 +236,12 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate, } } + @AppStorage(UserDefaultKey.textToSpeechPlaybackRate.rawValue) public var playbackRate = 1.0 { + didSet { + print("setting textToSpeechPlaybackRate: ", playbackRate) + } + } + public var currentVoice: String { get { UserDefaults.standard.string(forKey: Keys.textToSpeechVoice) ?? "en-US-JennyNeural" diff --git a/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift b/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift index 85707344d..a0418ec05 100644 --- a/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift +++ b/apple/OmnivoreKit/Sources/Utils/UserDefaultKeys.swift @@ -14,4 +14,5 @@ public enum UserDefaultKey: String { case lastUsedAppBuildNumber case lastItemSyncTime case audioInfoAlertShown + case textToSpeechPlaybackRate }