I need to download and process audio from an api that returns an url.
I can download the audio as data using the session object: }
@objc func download(surl: NSString, completion : @escaping (NSData) -> Void ) {
guard let url = URL(string: String(surl)) else { return }
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let task = session.dataTask(with: url) { data, response, error in
if let data = data, error == nil {
let nsdata = NSData(data: data)
DispatchQueue.main.async {
completion(nsdata)//returns the audio as NSData
}
} else {
//fail
}
}
task.resume()
}
With the resulting NSData
if it was an image I could do
if let img = UIImage(data: data as Data) {
do something with my image
}
However I'm unclear how best to hold and work with my audio data eg save, move etc.
Can you keep it in the form of anything more precise than Any or NSData
?
I believe you can play it as an url using avplayer:
let playerItem = AVPlayerItem.init(url: url)
player = AVPlayer.init(playerItem: playerItem)
player?.play()
However, is there a container equivalent to UIImage
or do you just deal with audio using urls, NSData Any
etc.
Thanks for any suggestions
The rough equivalent of UIImage
for audio is AVAsset
. It represents "the media" in a form independent of its format.
As a rule, you do not hold audio or video in a Data
because they are generally drastically larger than a single image and you will incur significant memory pressure. You can write them to disk and access them via a file URL.
Since holding audio or video as Data
is an extremely rare use case, there are few conveniences for it. You would need to manage it yourself as Data
and feed it to a AVSampleBufferAudioRenderer
.
On Mac, there is also NSSound
which supports constructing very small audio clips directly from Data
.