从 AppDelegate 播放声音

Play sound from AppDelegate

我正在为 iPhone 开发一个应用程序,因为我很懒,它使用网络视图和大量深层链接。我已经配置了应用程序的深度链接收听部分并且效果很好,但该方法似乎无法播放 systemsound 或 AVAudio。它没有错误,它只是不播放。我在方法中写了这个:

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()

这些声音在应用程序的另一部分播放正常,但在这里不行。它是否与它在 AppDelegate 中有关?这是怎么回事?记住这是 Swift

有可能对象在结束播放声音之前就被释放了。请在这里查看我的类似问题:

我已经按照建议创建了一个单例。

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.

如果有帮助请告诉我。