在XCode 6上,所有设备都可以正常运行,但是在模拟器上,声音无法播放。
有我的快速代码:
var url = NSURL(string: "http://my.url.com/sound.mp3")
var data = NSData(contentsOfURL: url!)
// Removed deprecated use of AVAudioSessionDelegate protocol
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
self.player = AVAudioPlayer(data: data, error: nil)
self.player.prepareToPlay()
self.player.delegate = self
self.player.volume = 1
self.player.play()
有日志:
ERROR: 98: Error '!obj' trying to fetch default input device's sample rate
ERROR: 100: Error getting audio input device sample rate: '!obj'
WARNING: 230: The input device is 0x0; '(null)'
WARNING: 234: The output device is 0x26; 'AppleHDAEngineOutputDP:3,0,1,1:0:{2D4C-092D-00000000}'
ERROR: 400: error '!obj'
ERROR: 113: * * * NULL AQIONode object
ERROR: 180: EXCEPTION thrown ('!dev'): -
ERROR: 703: Can't make UISound Renderer
ERROR: >aqsrv> 70: Exception caught in (null) - error -1
ERROR: 180: EXCEPTION thrown ('!dev'): -
ERROR: >aqsrv> 70: Exception caught in (null) - error -1
发生错误是因为您试图从模拟器获取device's sample rate
,这是不可能的。
因此某些硬件功能将无法使用它进行仿真。
它不能执行以下操作:
还有更多需要设备硬件加速的地方。
您需要在真实设备中进行测试。
[我遇到了这个问题,@Matti Jokipii的评论对我有很大帮助。
您需要启用声音输入,因此Mac不会为您提供null例外。转到系统偏好设置>声音。并检查是否已连接一些输入。如果没有,您将必须添加一个microfone,然后重新运行该应用程序。
我有这个问题。只需退出,然后再次启动Simulator。