Play sound from AppDelegate

1.1k views Asked by At

I'm working on an app for iPhone that uses a webview and lots of deeplinks because I'm lazy. I've configured the deeplink listening part of the app and that works great, but that method doesn't seem to be able to play systemsound or AVAudio. It doesn't error, it just doesn't play. I have this written in the method:

var iSaySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("isay", ofType: "mp3")!)
var youSaySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("yousay", ofType: "mp3")!)

var iSayPlayer = AVAudioPlayer(contentsOfURL: iSaySound, error: nil)
iSayPlayer.prepareToPlay()
iSayPlayer.play()

These sounds play in another part of the app fine, but not here. Does it have to do with it being in the AppDelegate? What's going on? Remember this is Swift

1

There are 1 answers

3
lchamp On BEST ANSWER

It is possible that the object is released before it ended playing the sound. Please check my similar question here : Swift - AVAudioPlayer, sound doesn't play correctly

Where I've created a singleton as it was suggested.

SoundPlayer.swift : http://pastebin.com/eX4iv3S4 .

To be used like so : SoundPlayer.sharedInstance.playSound().

Currently it use only one sound (because that is what I wanted), but you could easily add a parameter to the playSound method for example.

Let me know if it helped.