我正在开发一款 iPhone 应用程序,它使用 WebView 和大量深层链接,因为我很懒。我已经配置了应用程序的深度链接监听部分,效果很好,但该方法似乎无法播放系统声音或 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
最佳答案
该对象有可能在结束播放声音之前被释放。请在这里查看我的类似问题:Swift - AVAudioPlayer, sound doesn't play correctly
我按照建议创建了一个单例。
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.
请告诉我是否有帮助。
关于ios - 从 AppDelegate 播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30767176/