Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer sound not playing

In iOS 8/Xcode 6 I had a function that included a sound effect. It no longer works in iOS 9 after changing the code multiple times. This is what I've tried:

Original:

let bangSoundEffect = SKAction.playSoundFileNamed("Bang.mp3", waitForCompletion: false)
        runAction(bangSoundEffect)

Other attempt:

self.runAction(SKAction.playSoundFileNamed("Bang.mp3", waitForCompletion: false))

Also:

func playRocketExplosionSound(filename: String) {
            let url = NSBundle.mainBundle().URLForResource(
                filename, withExtension: nil)
            if (url == nil) {
                print("Could not find file: \(filename)")
                return }
            var error: NSError? = nil
            do {
                backgroundMusicPlayer =
                    try AVAudioPlayer(contentsOfURL: url!)
            } catch let error1 as NSError {
                error = error1
                backgroundMusicPlayer = nil
            }
            if backgroundMusicPlayer == nil {
                print("Could not create audio player: \(error!)")
                return}
            backgroundMusicPlayer.numberOfLoops = 1
            backgroundMusicPlayer.prepareToPlay()
            backgroundMusicPlayer.play() }



playRocketExplosionSound("Bang.mp3")

I'm pulling my hair out. I'm using the same code in a different scene for another sound effect and it works fine!! What's going wrong? I've noticed that the sound effect begins to play sometimes in the simulator, however it doesn't complete and throws this error:

2015-09-24 19:12:14.554 APPNAME[4982:270835] 19:12:14.553 ERROR:    177: timed out after 0.012s (735 736); mMajorChangePending=0

It doesn't work at all on actual devices.

What is the problem? :'(

like image 722
JGrn84 Avatar asked Sep 24 '15 18:09

JGrn84


1 Answers

Possible problem with MP3 file

The problem is most likely connected with the MP3 file you're using. The code works for other sounds, this suggests that the MP3 file might be corrupted and AVAudioPlayer fails with decoding it. You can try re-encode this file and see if the problem persists. Or, even better, converting it to WAV.

Using WAVs

General rule of the thumb when creating short sound effects for games, is to use WAV unless you really feel you need the trim the fat.

Top-notch games are going for top-of-the-line production quality, so they record and produce assets uncompressed 24bit/48kHz. Titles with slightly lesser ambitions might record and produce in 16/44.1, which is the official standard for CD quality audio.

This has at least two benefits. One is that the sound has a better quality. Second one, the CPU does not have to decode the file to play it.

like image 163
Rafa de King Avatar answered Nov 15 '22 19:11

Rafa de King