I'm attempting to load a remote MP3 through the AVAudioPlayer in Swift 4. So far, everything is working just great.
Now, I'm attempting to load our secure URL, which uses Basic Authentication for the URL.
What I have is this:
let username = "my_username"
let password = "my_password"
let loginString = String(format: "%@:%@", username, password)
let loginData = loginString.data(using: String.Encoding.utf8)!
let base64LoginString = loginData.base64EncodedString()
// create the request
let url = URL(string: "https://audio.my_domain.com/\(hashKey)/my_file.mp3")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
guard let audioPlayer = try? AVAudioPlayer(contentsOf: request) else {
self.endPlayback()
return
}
Of course the error that I get is:
Cannot convert value of type 'URLRequest' to expected argument type 'URL'
I'm not surprised by this as a URL is not a URLRequest object. So, my question is. How can I load the URL stream into the AVAudioPlayer using Basic HTTP Authentication?
I can't seem to find anywhere that does this. Perhaps this isn't possible?