How can I get AudioKit to stream an audio file from a remote URL?

元气小坏坏 提交于 2019-12-06 07:38:11

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!