Matej Ukmar
Matej Ukmar

Reputation: 2245

What is wrong with this URL for AVAudioPlayer in Swift?

I tried to create AVAudioPlayer like this

let url = URL(string: "https://storage.googleapis.com/preview-public/project/e45d3194bb7f4768984fd53acc833600/fa13293c27314b448a815ebd42176684/audio-gnvY.m4a")!
do {
  let player = try AVAudioPlayer(contentsOf: url)
} catch (let err) {
  print("err", err)
}

I get this error: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)".

I also tried

do {
  let _ = try url.checkResourceIsReachable()
} catch (let err) {
  print("err", err)
}

and I get this error: Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported.".

I can't figure out what could possibly be wrong with the url.

Images load normally from same bucket although if I check checkResourceIsReachable() on image URL it returns same error. (But I load image with CGImageSourceCreateWithURL(url as CFURL which might not have same checks as AVAudioPlayer.) Anyway I'm lost. Any ideas?

Upvotes: 0

Views: 1016

Answers (3)

Saurav_Sharma
Saurav_Sharma

Reputation: 140

In AVAudioPlayer, URL is the path to a local file.

https://developer.apple.com/documentation/foundation/url

Upvotes: 0

Himanshu Patel
Himanshu Patel

Reputation: 1033

i have demo in try to run this code audio is working perfect

 let audiourl = URL(string: "https://storage.googleapis.com/preview-public/project/e45d3194bb7f4768984fd53acc833600/fa13293c27314b448a815ebd42176684/audio-gnvY.m4a")
    let player = AVPlayer(url: audiourl!)
    let playViewController = AVPlayerViewController()
    playViewController.player = player
    self.present(playViewController, animated: true){
        playViewController.player!.play()
    }

Upvotes: 1

matt
matt

Reputation: 535192

AVAudioPlayer is for local file: URLs only. It doesn’t accept remote URLs, which would need to be streamed. For that, use AVPlayer.

Upvotes: 1

Related Questions