A Swift class for downloading and caching audio.
Supports iOS, iPadOS, macOS, tvOS.
Fetch audio from a remote URL with:
AudioCache.shared.fetchAudio(from: URL) async throws -> (URL, Data)
This stores the audio in the cache directory and returns a tuple with:
- the URL to the cached file, and
- a Data instance with the contents.
Further calls return the cached file without fetching from the remote URL.
The cached file can be removed using removeCachedFile(for:)
with the original remote URL as the key:
AudioCache.shared.removeCachedFile(for: URL) async throws
// Load the audio requested by the user
let url = URL(string: "http://...")
let (localURL, audioData) = try await AudioCache.shared.fetchAudio(from: url)
...
// When the user is finished with the audio
AudioCache.shared.removeCachedFile(for: url)
The cached files are stored in the Application Support directory, in a folder named "Audio Cache".
This folder name is stored in AudioCache.shared.cacheFolderName
.
Note: Do not localise this string, as the cache would change location, leaving orphaned files.
AudioCache.shared.cacheFolderName = "My cache folder"
The URLSession can be set. It defaults to URLSession.shared
.
AudioCache.shared.urlSession = myURLSession
To add AudioCache as a Package Dependancy:
- With your project open in Xcode, select File → Add Package Dependancies….
- Paste the GitHub repo URL
https://github.com/garymakin/AudioCache.git
- Click the Copy Dependancy button.
The packages can be managed in Xcode, along side the Build Settings for the project.