diff --git a/ios/App/App/plugins/AbsDownloader.swift b/ios/App/App/plugins/AbsDownloader.swift index 186b0e24..a4c13f45 100644 --- a/ios/App/App/plugins/AbsDownloader.swift +++ b/ios/App/App/plugins/AbsDownloader.swift @@ -347,10 +347,21 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate { } private func urlForTrack(item: LibraryItem, track: AudioTrack) -> URL { - // filename needs to be encoded otherwise would just use contentUrl - let relPath = track.metadata?.relPath ?? "" - let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) - let urlstr = "\(Store.serverConfig!.address)/s/item/\(item.id)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)" + // TODO: Future server release should include ino with AudioFile or FileMetadata + let trackPath = track.metadata?.path ?? "" + + var audioFileIno = "" + if (item.mediaType == "podcast") { + let podcastEpisodes = item.media?.episodes ?? List() + let matchingEpisode = podcastEpisodes.first(where: { $0.audioFile?.metadata?.path == trackPath }) + audioFileIno = matchingEpisode?.audioFile?.ino ?? "" + } else { + let audioFiles = item.media?.audioFiles ?? List() + let matchingAudioFile = audioFiles.first(where: { $0.metadata?.path == trackPath }) + audioFileIno = matchingAudioFile?.ino ?? "" + } + + let urlstr = "\(Store.serverConfig!.address)/api/items/\(item.id)/file/\(audioFileIno)/download?token=\(Store.serverConfig!.token)" return URL(string: urlstr)! } diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index 81489715..a0a56aed 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -9,6 +9,7 @@ import Foundation import AVFoundation import UIKit import MediaPlayer +import RealmSwift enum PlayMethod: Int { case directplay = 0 @@ -86,7 +87,21 @@ class AudioPlayer: NSObject { self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem), options: .new, context: &playerContext) for track in playbackSession.audioTracks { - if let playerAsset = createAsset(itemId: playbackSession.libraryItemId!, track: track) { + + // TODO: All of this to get the ino of the file on the server. Future server release will include the ino with the session tracks + var audioFileIno = "" + let trackPath = track.metadata?.path ?? "" + if (!playbackSession.isLocal && playbackSession.episodeId != nil) { + let episodes = playbackSession.libraryItem?.media?.episodes ?? List() + let matchingEpisode:PodcastEpisode? = episodes.first(where: { $0.audioFile?.metadata?.path == trackPath }) + audioFileIno = matchingEpisode?.audioFile?.ino ?? "" + } else if (!playbackSession.isLocal) { + let audioFiles = playbackSession.libraryItem?.media?.audioFiles ?? List() + let matchingAudioFile = audioFiles.first(where: { $0.metadata?.path == trackPath }) + audioFileIno = matchingAudioFile?.ino ?? "" + } + + if let playerAsset = createAsset(itemId: playbackSession.libraryItemId!, track: track, ino: audioFileIno) { let playerItem = AVPlayerItem(asset: playerAsset) if (playbackSession.playMethod == PlayMethod.transcode.rawValue) { playerItem.preferredForwardBufferDuration = 50 @@ -483,23 +498,18 @@ class AudioPlayer: NSObject { } // MARK: - Private - private func createAsset(itemId:String, track:AudioTrack) -> AVAsset? { + private func createAsset(itemId:String, track:AudioTrack, ino:String) -> AVAsset? { guard let playbackSession = self.getPlaybackSession() else { return nil } if (playbackSession.playMethod == PlayMethod.directplay.rawValue) { - // The only reason this is separate is because the filename needs to be encoded - let relPath = track.metadata?.relPath ?? "" - let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) - let urlstr = "\(Store.serverConfig!.address)/s/item/\(itemId)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)" + let urlstr = "\(Store.serverConfig!.address)/api/items/\(itemId)/file/\(ino)?token=\(Store.serverConfig!.token)" let url = URL(string: urlstr)! return AVURLAsset(url: url) } else if (playbackSession.playMethod == PlayMethod.local.rawValue) { guard let localFile = track.getLocalFile() else { // Worst case we can stream the file logger.log("Unable to play local file. Resulting to streaming \(track.localFileId ?? "Unknown")") - let relPath = track.metadata?.relPath ?? "" - let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) - let urlstr = "\(Store.serverConfig!.address)/s/item/\(itemId)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)" + let urlstr = "\(Store.serverConfig!.address)/api/items/\(itemId)/file/\(ino)?token=\(Store.serverConfig!.token)" let url = URL(string: urlstr)! return AVURLAsset(url: url) }