0

I'm trying to play an m4a file on an iOS device programatically. The file is downloaded from a server and stored. When the user presses a button, it is played. The file, when copied from the device to OSX does play fine. I am using the code

var player:AVAudioPlayer = AVAudioPlayer()

func playAudio(sender: UIButton!) {
    let audioPath = FileUtils.getPath(audioPaths[sender.tag])
    print("PATH " + audioPath)//The printed path IS DEFINETLY correct
    do{
        player = try! AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
        player.prepareToPlay()
        player.play()
    }
}

The path is correct so I'm not sure why it won't work. Could it be the format (even though it plays on OSX)?

An example file: https://www.dropbox.com/s/csewwg6n9vzan5z/131015-08%3A13%3A30.m4a?dl=0

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46

2 Answers2

5

That file won't play on iOS because it's encoded in AMR NarrowBand, which is no longer supported.

I would try re-encoding the m4a files as AAC, or switch to mp3.

Community
  • 1
  • 1
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Im going to accept this answer because it's the one which helped both, but @matt you were helpful too, I can only choose one though – Hamzah Malik Oct 14 '15 at 19:14
1

It's likely just a feature of the way this m4a file is compressed. The simple fact is that not every m4a file, even if it is a perfectly valid file, will play on your device. (For example, perhaps this file is very compressed.) And to make things more complicated, it differs from device to device! And to make things even more complicated, the file might play in the Music app but not in an AVAudioPlayer!!

To test this theory, recode the file as a more middle-of-the-road m4a and see if that plays.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Makes sense but is there no workaround? The fact that osx can play the file proves its possible – Hamzah Malik Oct 12 '15 at 21:33
  • I don't think you're listening to what I'm saying. The fact that OS X can play the file _is the problem_. This is a file that OS X can play _but your iOS device cannot_. Trust me. I've had it happen to me. – matt Oct 13 '15 at 00:02
  • @HamzahMalik Are you starting to believe me now? – matt Oct 14 '15 at 01:36
  • What I'm getting at is: the file is a valid m4a but not supported. How can I make it supported? (Libraries, different encoding etc) – Hamzah Malik Oct 14 '15 at 06:23