Hamzah Malik
Hamzah Malik

Reputation: 2570

Swift 2 - m4a file won't play on iOS

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

Upvotes: 0

Views: 2770

Answers (2)

Gordon Childs
Gordon Childs

Reputation: 36072

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.

Upvotes: 5

matt
matt

Reputation: 534925

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.

Upvotes: 1

Related Questions