0

I want to play a sound in my application. Nothing special, just a little WAV file. The file exists in the device in my main bundle, and I've got both the AVFoundation and AudioToolbox frameworks added. I have alternately tried using both of them, testing out every bit of sample code on the net for playing this sound. It won't work. So what else could be wrong, given that the file exists and the code snippet probably isn't the problem?

  1. Is it to do with imports? I import the necessary .h files for the frameworks at the top of the file.
  2. I've noticed that one of the methods requires there to be a delegate that implements a particular protocol. Is this necessary? Some examples require it, others don't. What do I need to implement for this?
  3. This fails to play the sound both on the device and the simulator. HAve I overlooked antyhing else?
3
  • Have you played the sound on the computer to ensure it works? Is the sound file just silence? Is your volume turned down? Is the iPhone in silent mode?
    – Jasarien
    Aug 10, 2010 at 11:36
  • Nope, it's all good. The sound file plays on the machine, and the iPad has the volume whacked way up. I'm just checking initialisation now. The audioPlayer inits with no errors, but claims its 'data' is (null) when I try to toString it.
    – mtrc
    Aug 10, 2010 at 11:54
  • You should add to the answer the code where you configure the AVAudioPlayer. If the problem is not with the file, it will be there.
    – TechZen
    Aug 10, 2010 at 12:21

2 Answers 2

2
Is it to do with imports? 

No. Problems with imports will be apparent at build time.

I've noticed that one of the methods requires there to be a delegate that implements a particular protocol. Is this necessary?

No. The AVAudioPlayer delegate is optional.

This fails to play the sound both on the device and the simulator. HAve I overlooked antyhing else?

The most likely explanation is that the file is not being properly referenced. Alternatively, the file may be in a non-standard format for and the player chokes on it silently. Try testing with another file type.

2
  • Oh god, an mp3 file worked. I guess the WAV is just encoded in a not-nice way or something. Bloody silent choking. Why can't they splutter loudly like everyone else! Thanks!
    – mtrc
    Aug 10, 2010 at 12:38
  • Silent failures are evil. In this case, the player most likely interpreted the file as being empty owing to the file having improper offset value for the data block. Since its not illegal to have an empty data block, the player didn't complain.
    – TechZen
    Aug 10, 2010 at 12:50
0

In my case today, an mp3 worked fine on the Simulator (and in iTunes and everywhere else I tested it), but not on a device. I converted the mp3 to an aif and IT worked fine on the device.

I know this is more worthy of a comment than an answer, but I lack the rep to comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.