Skip to content

Commit

Permalink
Audio player for PDF book #57: Store/restore the last audio
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Nov 29, 2019
1 parent 16cf379 commit d7fbd09
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions ReaderTranslator/Stores/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Store: ObservableObject {

@Published var currentPdfPage = "1"
@Published var pageCount = 0
@Published(wrappedValue: nil, key: "pdfAudio") var pdfAudio: URL?

@Published(key: "currentTab") var currentTab = 0 {
didSet { self.lastWebPage = self.savedLastWebPage[self.currentTab] }
Expand Down
58 changes: 37 additions & 21 deletions ReaderTranslator/Views/ReaderView/Modes/PdfToolbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,39 @@ struct PdfToolbarView: View {
Button(action: {
OpenPanel.showChooseFileDialog(title: "Open audio file", allowedFileTypes: ["mp3", "mov"]) { url in
guard let url = url else { return }
do {
player = try AVAudioPlayer(contentsOf: url)
player?.enableRate = true
} catch {
print(error)
}
self.store.pdfAudio = url
self.openAudio(url: url)
self.audioRate = 1
}
}, label: { Text("Open audio") })

Text("\(currentStatus)").frame(width: 100)

Group {
Button(action: { player?.currentTime = 0 }, label: { Text("|<") })
Button(action: { player?.currentTime -= 100 }, label: { Text("-100") })
Button(action: { player?.currentTime -= 10 }, label: { Text("-10") })
Button(action: { player?.currentTime -= 5 }, label: { Text("-5") })
Button(action: { player?.currentTime -= 1 }, label: { Text("-1") })
rewindButton(label: "-100", step: -100)
rewindButton(label: "-10", step: -10)
rewindButton(label: "-5", step: -5)
rewindButton(label: "-1", step: -1)
}

Button(action: {
if self.isPlaying { player?.pause() } else { player?.play() }
}, label: { Text(isPlaying ? "Pause" : "Play") })
guard let player = player else { return }
if self.isPlaying {
player.pause()
} else {
//hack: currentTime jump forward for some time after an audio is continue to play
let currentTime = player.currentTime
player.play()
player.currentTime = currentTime
}
}, label: { Text(isPlaying ? "Pause" : "Play ") })

Group {
Button(action: { player?.currentTime += 1 }, label: { Text("+1") })
Button(action: { player?.currentTime += 5 }, label: { Text("+5") })
Button(action: { player?.currentTime += 10 }, label: { Text("+10") })
Button(action: { player?.currentTime += 100 }, label: { Text("+100") })
rewindButton(label: "+1", step: 1)
rewindButton(label: "+5", step: 5)
rewindButton(label: "+10", step: 10)
rewindButton(label: "+100", step: 100)
}

Group {
Expand All @@ -72,19 +79,28 @@ struct PdfToolbarView: View {
Text(String(format: "%.1f", arguments: [audioRate]))
Button(action: { self.audioRate += 0.1 }, label: { Text("+") })
}

Group {
Divider()
Text("\(currentStatus)")
}
}
.onAppear {
_ = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
guard let player = player else { return }
self.currentStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
self.isPlaying = player.isPlaying
}
if let url = self.store.pdfAudio { self.openAudio(url: url) }
}
}

private func rewindButton(label: String, step: Double) -> some View {
Button(action: { player?.currentTime += step }, label: { Text(label) })
}

private func openAudio(url: URL) {
do {
player = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
player?.enableRate = true
}
}

Expand Down
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>223</string>
<string>240</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>297</string>
<string>317</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSAppleEventsUsageDescription</key>
Expand Down

0 comments on commit d7fbd09

Please sign in to comment.