0

i just want to ask on how to play audio using avAudion using url link..

and im getting error bad request.. can someone help me?

i already tried using avplayer but it is not suitable for me.. though it is working but.. i kinda prefer to use avaudioplayer

    let mp3URL = NSURL(fileURLWithPath:"https://s3.amazonaws.com/kargopolov/kukushka.mp3")
    do {
        // 2
        audioPlayer = try AVAudioPlayer(contentsOf: mp3URL as URL)
        audioPlayer.play()
        // 3
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateAudioProgressView), userInfo: nil, repeats: true)
        progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: false)
    }
    catch {
            print("An error occurred while trying to extract audio file")
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jore Dawal
  • 59
  • 1
  • 10

2 Answers2

2

According to the AVAudioPlayer documentation, if you want to play audio over the internet (that's not already downloaded to disk or memory) you should use AVPlayer.

The docs describe AVAudioPlayer as

An audio player that provides playback of audio data from a file or memory.

A little further down, in the overview section, emphasis added:

Use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.


Notes

  1. You can get some more information from the runtime by accessing the error object that is thrown when the connection fails. Change catch { to catch let error { and then you can log out the error as part of your message, like so:
catch let error{
    print("An error occurred while trying to extract audio file: \(error)")
}
  1. When I run your sample code in Xcode playgrounds with the change noted above, I see the following:

An error occurred while trying to extract audio file: Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)"

  1. Notice the error's Domain and Code. Pasting that error into Google yields some results that indicate that the URL might not be resolving correctly. (Indeed, clicking on that mp3 link shows a bad access error message.)

The first result is another StackOverflow post with a similar issues. The second one is an Apple Developer Forum post which has some more information.

Let's try to change the URL to a publicly accessible sample mp3 file. (I found this one by searching the web for "test mp3 file" on Google.)

  1. You'll want to change NSURL to URL, and instead of fileURLWithPath, you're going to want to use another initializer. (Say, string:.)

  2. Whenever you see contentsOf...: in a media or file API, there's a good chance it expects data or a file, to the exclusion of a network stream. Similarly, when you see an initializer or method that takes a fileURL..., the system expects to be pointing to a local resource, not a network URL.

Moshe
  • 57,511
  • 78
  • 272
  • 425
-3
if let mp3URL = URL(string: "https://s3.amazonaws.com/kargopolov/kukushka.mp3"){
        // do s.t
}   

Anyway, you should know the basics of ios

Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18