Skip to content

Commit

Permalink
Audio player for PDF book #57: Update the status after opening audio.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 11, 2019
1 parent 54feef0 commit d14158a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
5 changes: 5 additions & 0 deletions ReaderTranslator/Components/AudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ struct AudioPlayer {
String(format: "%.1f", arguments: [rate])
}

var status: String {
guard let player = player else { return "" }
return String(format: "%.1f/%.1f", player.currentTime, player.duration)
}

mutating func openAudio(url: URL) {
do {
self.player = try AVAudioPlayer(contentsOf: url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct ReaderView_Pdf_Toolbar: View {
var body: some View {
VStack {
ReaderView_Pdf_Toolbar_PlayButtons(
player: $player,
audioPlayer: $player,
currentStatus: $currentStatus,
isPlaying: $isPlaying)
HStack {
Expand Down Expand Up @@ -57,6 +57,7 @@ struct ReaderView_Pdf_Toolbar: View {
self.store.pdfAudio = url
self.player.openAudio(url: url)
self.player.rate = 1
self.currentStatus = self.player.status
}
}, label: { Text("📂 audio") })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
private var timer: Timer?

struct ReaderView_Pdf_Toolbar_PlayButtons: View {
@Binding var player: AudioPlayer
@Binding var audioPlayer: AudioPlayer
@Binding var currentStatus: String
@Binding var isPlaying: Bool {
willSet {
Expand All @@ -27,7 +27,7 @@ struct ReaderView_Pdf_Toolbar_PlayButtons: View {

var body: some View {
HStack(spacing: 2) {
Button(action: { self.player.player?.currentTime = 0 }, label: { Text("|<") })
Button(action: { self.audioPlayer.player?.currentTime = 0 }, label: { Text("|<") })
rewindButton(label: "-50", step: -50)
rewindButton(label: "-5", step: -5)
rewindButton(label: "-1", step: -1)
Expand All @@ -39,7 +39,7 @@ struct ReaderView_Pdf_Toolbar_PlayButtons: View {
rewindButton(label: "+50", step: 50)
}
.onAppear {
if let url = self.store.pdfAudio { self.player.openAudio(url: url) }
if let url = self.store.pdfAudio { self.audioPlayer.openAudio(url: url) }
}
.onDisappear {
timer?.invalidate()
Expand All @@ -48,7 +48,7 @@ struct ReaderView_Pdf_Toolbar_PlayButtons: View {

private var playButtonView: some View {
Button(action: {
guard let player = self.player.player else { return }
guard let player = self.audioPlayer.player else { return }
if self.isPlaying {
player.pause()
} else {
Expand All @@ -63,23 +63,18 @@ struct ReaderView_Pdf_Toolbar_PlayButtons: View {

private func rewindButton(label: String, step: Double) -> some View {
Button(action: {
self.player.player?.currentTime += step
self.updateStatus()
self.audioPlayer.player?.currentTime += step
self.currentStatus = self.audioPlayer.status

}, label: { Text(label) })
}

private func startTimer() {
timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
guard let player = self.player.player else { return }
self.updateStatus()
guard let player = self.audioPlayer.player else { return }
self.currentStatus = self.audioPlayer.status
player.volume = self.store.voiceVolume
if self.isPlaying != player.isPlaying { self.isPlaying = player.isPlaying }
}
}

private func updateStatus() {
guard let player = self.player.player else { return }
self.currentStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
}
}
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>1017</string>
<string>1022</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorSafari/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>1624</string>
<string>1630</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit d14158a

Please sign in to comment.