From f7d5c129fc5e5626686205dcfbd921219bc8e970 Mon Sep 17 00:00:00 2001 From: Krishna Kumar Date: Sat, 23 May 2026 00:51:23 -0500 Subject: [PATCH] =?UTF-8?q?experiment(PodcastPlayerView):=20add=20playback?= =?UTF-8?q?Speed=20control=20(0.75=C3=97/1=C3=97/1.25=C3=97/1.5=C3=97/2?= =?UTF-8?q?=C3=97)=20with=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marks/Views/PodcastPlayerView.swift | 40 +++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/Marks/Views/PodcastPlayerView.swift b/Marks/Views/PodcastPlayerView.swift index 7671e21..0a96c7c 100644 --- a/Marks/Views/PodcastPlayerView.swift +++ b/Marks/Views/PodcastPlayerView.swift @@ -42,6 +42,9 @@ final class PodcastPlayerViewModel { var duration: Double = 1 var currentArticleUrl: String = "" var currentArticleTitle: String = "" + var playbackSpeed: Float = 1.0 + + static let availableSpeeds: [Float] = [0.75, 1.0, 1.25, 1.5, 2.0] var isGenerating: Bool { switch phase { @@ -135,7 +138,11 @@ final class PodcastPlayerViewModel { func togglePlayPause() { guard let player else { return } - if isPlaying { player.pause() } else { player.play() } + if isPlaying { + player.pause() + } else { + player.rate = playbackSpeed + } isPlaying.toggle() updateNowPlaying() } @@ -154,6 +161,12 @@ final class PodcastPlayerViewModel { seek(to: max(currentTime - 15, 0)) } + func setPlaybackSpeed(_ speed: Float) { + playbackSpeed = speed + if isPlaying { player?.rate = speed } + updateNowPlaying() + } + private func setupPlayer(url: URL, title: String) { currentPodcastTitle = title @@ -205,7 +218,7 @@ final class PodcastPlayerViewModel { setupRemoteCommands() phase = .ready(title: title) updateNowPlaying() - p.play() + p.rate = playbackSpeed isPlaying = true } @@ -384,6 +397,29 @@ struct PodcastPlayerView: View { .foregroundStyle(.primary) } } + + Menu { + ForEach(PodcastPlayerViewModel.availableSpeeds, id: \.self) { speed in + Button { + vm.setPlaybackSpeed(speed) + } label: { + let label = speed == 1.0 ? "1× (Normal)" : "\(String(format: "%g", speed))×" + if speed == vm.playbackSpeed { + Label(label, systemImage: "checkmark") + } else { + Text(label) + } + } + } + } label: { + Text(vm.playbackSpeed == 1.0 ? "1× Speed" : "\(String(format: "%g", vm.playbackSpeed))×") + .font(.system(size: 14, weight: .semibold)) + .foregroundStyle(.secondary) + .padding(.horizontal, 14) + .padding(.vertical, 7) + .background(Color(.systemGray5)) + .clipShape(Capsule()) + } } }