0

I am trying to play the audio from remote URL.

let audioData = try! Data.init(contentsOf: URL)
do       
 {
      self.audioPlayer = try AVAudioPlayer.init(data: data) //Throwing error sometimes
      self.audioPlayer?.delegate = self
      self.audioPlayer?.prepareToPlay()     
      self.audioPlayer?.play()

 }
 catch {
          showErrorMessage("An error occurred while trying to extract audio file")

  }

I have a list of audio url on my server. for some audio I am able to get the Data but in the AVAudioPlayer's init method it is throwing the error. I am not able to get the actual cause for this.

I have the option of AVPlayer but why this thing is causing the issue?

Bindiya
  • 614
  • 2
  • 9
  • 23

4 Answers4

3

I try it by the code below and its work fine

  let audioData = try! Data.init(contentsOf: url)
    do
    {
        let audioPlayer = try AVAudioPlayer.init(data: audioData) //Throwing error sometimes
        audioPlayer.delegate = self as? AVAudioPlayerDelegate
        audioPlayer.prepareToPlay()
        audioPlayer.play()

    } catch {
        print("An error occurred while trying to extract audio file")
    }
A.Munzer
  • 1,890
  • 1
  • 16
  • 27
  • Yes , It works fine with most of the urls but we are using some audio jungle urls which are causing the issue. – Bindiya Apr 24 '18 at 07:24
  • Or If you want to try with this specific url which is causing Issue - https://s3.envato.com/files/142154780/preview.mp3 – Bindiya Apr 24 '18 at 07:49
2

Just directly Play Audio From URL you have as

do
{
  try Manager.player = AVAudioPlayer.init(contentsOf: returnPathAtSelectedIndex(fileName: fileName))
  //Set required delegates and Values

  Manager.player?.delegate = self
  Manager.player?.volume = 1.0
  Manager.player?.prepareToPlay()
  Manager.player?.play()
}
catch
{
  print("Error while playing music: \(error.localizedDescription)")
}

My Player

struct Manager 
{
   ///AVAudio Player
    static var player: AVAudioPlayer?
}

My function func returnPathAtSelectedIndex(fileName:String) -> URL Returns a URL.

Edit

As the AVAudioPlayer plays the file saved into the local. You need to save the file to local and than

AVAudioPlayer.init(contentsOf:localFileUrl)

will help to play the file and won't generate the crash.

Bindiya
  • 614
  • 2
  • 9
  • 23
iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • I have to use that audio file later so downloading the audio to `Data` is compulsory. – Bindiya Apr 24 '18 at 07:33
  • You can Manage the download procedure as a Separate Thread, Like BackGround downloading, URL can be used to play file as one thread, and use Data of URL as Separate Thread , So load wont be on one Controller only , Hope you getting my point – iOS Geek Apr 24 '18 at 09:30
  • Yes I tried with local audio file URL and It worked with me. – Bindiya Apr 24 '18 at 10:30
  • if this helped you and query is solved you can close this thread by accepting answer – iOS Geek Apr 25 '18 at 04:11
1

Please change your code to this, so you can know more what kind of error is happening:

let audioData = try! Data.init(contentsOf: URL)
do {
   self.audioPlayer = try AVAudioPlayer.init(data: data) //Throwing error sometimes
   self.audioPlayer?.delegate = self
   self.audioPlayer?.prepareToPlay()     
   self.audioPlayer?.play()

}
catch let error {
   print(error.localizedDescription)
   showErrorMessage(error.localizedDescription)

}
vicegax
  • 4,709
  • 28
  • 37
0

After debugging the localised description I was getting the error

The operation couldn’t be completed. (OSStatus error 1954115647.)

and below link helped me to resolve my issue.

https://stackoverflow.com/a/5252724/5227570

Instead of initializing the audio player with the NSData object, save the file to the Documents folder, and then initialized the player with the file URL

Bindiya
  • 614
  • 2
  • 9
  • 23