问题
I'm trying to play audio files from URLs in an iOS app (Swift 4). I'd like them to buffer and play while downloading. And I quite like AudioKit, but for the life of me and I can't figure out how to make it read remote files.
Any suggestions?
回答1:
You can't find it because its not there. We've never implemented any streaming functionality. It could be added though, and a number of people like yourself would be quite pleased. Consider contributing some code if you manage to work it out. We can offer you membership to AudioKit's Slack group during development if you wish.
回答2:
If you only need to stream and play audio files you might just use AVFoundation.AVPlayer. If you want to integrate it into AudioKit then more is needed currently.
You can cache the URL of the remote file locally then load that cached file into AKPlayer. Obviously not streaming - but it will work:
guard let remote = URL(string: "https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3"),
let data = NSData(contentsOf: remote) else {
AKLog("Remote failed to load.")
return
}
let cachedFile = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(remote.lastPathComponent)
try? data.write(to: cachedFile)
let player = AKPlayer(url: cachedFile)
Note that this is just a quick macOS sample - you will likely want to write to a temp directory or some other place.
来源:https://stackoverflow.com/questions/52232378/how-can-i-get-audiokit-to-stream-an-audio-file-from-a-remote-url