Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a Remote URL #22

Closed
daihovey opened this issue Mar 1, 2021 · 1 comment
Closed

Load a Remote URL #22

daihovey opened this issue Mar 1, 2021 · 1 comment

Comments

@daihovey
Copy link

daihovey commented Mar 1, 2021

Has anyone attempted to build a version which could load a remote URL?
AVAssetReader complains currently "Cannot initialize an instance of AVAssetReader with an asset at non-local URL"

@dmrschmidt
Copy link
Owner

I've never actually tried that myself tbh. But you can simply download the file with URLSession. Code for this (just an illustration as it ignores all sorts of possible error scenarios) could look like this:

let task = URLSession.shared.downloadTask(with: remoteAudioURL) { downloadedURL, urlResponse, error in
    guard let downloadedURL = downloadedURL else { return }

    let cachesFolderURL = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let audioFileURL = cachesFolderURL!.appendingPathComponent("yourLocalAudioFile.m4a")
    try? FileManager.default.copyItem(at: downloadedURL, to: audioFileURL)

    DispatchQueue.main.async {
        self.WaveformView.waveformAudioURL = audioFileURL
    }
}
task.resume()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants