ios - 从 AppDelegate 播放声音

标签 ios swift avfoundation

我正在开发一款 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/

相关文章:

ios - Swift - 使用 AVPlayerItemVideoOutput 访问解码帧 : outputMediaDataWillChange is not called

ios - Swift:如何从使用 AVFoundation 拍摄的照片中删除 EXIF 数据?

ios - 无法导入桥接头 - Swift

ios方法参数分配全局变量

ios - 无法使用 Today Extension 的共享应用组共享数据

ios - 为什么使用照片框架的示例应用程序在每次更改后都使用 stopCachingImagesForAllAssets?

ios - 在模拟器中测试多任务 iOS 9 功能

ios - MKPointAnnotation 标签

ios - 在后台运行的 swift WatchConnectivity - ApplicationContext 并非一直运行

objective-c - 捕获音频/视频后无法播放系统声音