Skip to content

Commit

Permalink
AudioPlayer features #58: Update the timeline after pressing the rewi…
Browse files Browse the repository at this point in the history
…nd button while pausing.
  • Loading branch information
filimo committed Jan 12, 2020
1 parent 55ed488 commit b8797a4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1762</string>
<string>1768</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorPlayer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>29</string>
<string>35</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
Expand Down
14 changes: 9 additions & 5 deletions ReaderTranslatorPlayer/Store/AudioStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class AudioStore: NSObject, ObservableObject {

@Published private(set) var allAudioPlayers = [AVAudioPlayer]()

@Published var currentStatus = "0.0/0.0"
@Published var timelineStatus = "0.0/0.0"
@Published var isPlaying = false {
willSet {
guard let player = player else { return }
Expand Down Expand Up @@ -68,6 +68,13 @@ final class AudioStore: NSObject, ObservableObject {
}
}

extension AudioStore {
func updateTimeline(timer _: Timer? = nil) {
guard let player = self.player else { return }
self.timelineStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
}
}

extension AudioStore {
func openAudio(url: URL?) {
guard let url = url else { return }
Expand Down Expand Up @@ -173,10 +180,7 @@ extension AudioStore {

func startTimer() {
invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
guard let player = self.player else { return }
self.currentStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
}
timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: updateTimeline(timer:))
}

func setSleepTimer(minutes: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PlayerControlsView: View {

var body: some View {
VStack(spacing: 10) {
Text("\(audioStore.currentStatus)")
Text("\(audioStore.timelineStatus)")
AudioRateView()
RewindButtonsView()
HStack(spacing: 20) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import SwiftUI

struct RewindButtonsView: View {
let audioStore = AudioStore.shared

var body: some View {
HStack {
Button(
action: { AudioStore.shared.player?.currentTime = 0 },
label: { Text("|<") })
.buttonStyle(RoundButtonStyle())
label: { Text("|<") }
)
.buttonStyle(RoundButtonStyle())
rewindButton(label: "-50", step: -50)
rewindButton(label: "-5", step: -5)
rewindButton(label: "-1", step: -1)
Expand All @@ -26,7 +29,10 @@ struct RewindButtonsView: View {

private func rewindButton(label: String, step: Double) -> some View {
Button(
action: { AudioStore.shared.player?.currentTime += step },
action: {
self.audioStore.player?.currentTime += step
self.audioStore.updateTimeline()
},
label: { Text(label).frame(width: 35) }
)
.buttonStyle(RoundButtonStyle())
Expand Down

0 comments on commit b8797a4

Please sign in to comment.