Merge pull request #1153 from omnivore-app/fix/download-speechmarks

Add audio download types so we can also download speechmarks files
This commit is contained in:
Jackson Harper
2022-08-30 18:26:08 +08:00
committed by GitHub
2 changed files with 16 additions and 9 deletions

View File

@ -55,7 +55,7 @@ import Views
items.insert(item, at: 0)
}
func loadItems(dataService: DataService, audioSession: AudioSession, isRefresh: Bool) async {
func loadItems(dataService: DataService, audioSession _: AudioSession, isRefresh: Bool) async {
let syncStartTime = Date()
let thisSearchIdx = searchIdx
searchIdx += 1
@ -123,7 +123,7 @@ import Views
cursor = queryResult.cursor
if let username = dataService.currentViewer?.username {
await dataService.prefetchPages(itemIDs: newItems.map(\.unwrappedID), username: username)
await audioSession.preload(itemIDs: newItems.map(\.unwrappedID))
// await audioSession.preload(itemIDs: newItems.map(\.unwrappedID))
}
} else {
updateFetchController(dataService: dataService)

View File

@ -25,9 +25,14 @@ public enum PlayerScrubState {
case scrubEnded(TimeInterval)
}
enum DownloadType: String {
case mp3
case speechMarks
}
enum DownloadPriority: String {
case low = "low"
case high = "high"
case low
case high
}
// Our observable object class
@ -82,7 +87,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
}
// Attempt to fetch the file if not downloaded already
let result = try? await downloadAudioFile(pageId: pageId, priority: .low)
let result = try? await downloadAudioFile(pageId: pageId, type: .mp3, priority: .low)
if result == nil {
print("audio file had error downloading: ", pageId)
pendingList.append(pageId)
@ -173,7 +178,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
let pageId = item!.unwrappedID
downloadTask = Task {
let result = try? await downloadAudioFile(pageId: pageId, priority: .high)
let result = try? await downloadAudioFile(pageId: pageId, type: .mp3, priority: .high)
if Task.isCancelled { return }
if result == nil {
@ -222,6 +227,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
}
} catch {
print("error playing MP3 file", error)
try? FileManager.default.removeItem(atPath: audioUrl.path)
state = .stopped
}
}
@ -352,14 +358,15 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
}
}
func downloadAudioFile(pageId: String, priority: DownloadPriority) async throws -> (pending: Bool, url: URL?) {
func downloadAudioFile(pageId: String, type: DownloadType, priority: DownloadPriority) async throws -> (pending: Bool, url: URL?) {
let audioUrl = pathForAudioFile(pageId: pageId)
if FileManager.default.fileExists(atPath: audioUrl.path) {
return (pending: false, url: audioUrl)
}
guard let url = URL(string: "/api/article/\(pageId)/mp3/\(priority)/\(currentVoice)", relativeTo: appEnvironment.serverBaseURL) else {
let path = "/api/article/\(pageId)/\(type)/\(priority)/\(currentVoice)"
guard let url = URL(string: path, relativeTo: appEnvironment.serverBaseURL) else {
throw BasicError.message(messageText: "Invalid audio URL")
}
@ -374,7 +381,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate {
guard let httpResponse = result?.1 as? HTTPURLResponse, 200 ..< 300 ~= httpResponse.statusCode else {
throw BasicError.message(messageText: "audioFetch failed. no response or bad status code.")
}
print("httpResponse: ", httpResponse)
if let httpResponse = result?.1 as? HTTPURLResponse, httpResponse.statusCode == 202 {
return (pending: true, nil)
}