您的位置:首页 > 移动开发 > IOS开发

Xcode模拟器中无法播放音频文件的原因分析

2015-10-05 07:51 447 查看
在本猫的Mac Mini上开发iOS app,发现当执行到播放音频的代码时,发生错误,log如下:

2015-10-05 07:22:17.122 LearnSpriteBuilder[10321:559370] 07:22:17.122 ERROR:     98: Error '!obj' trying to fetch default input device's sample rate
2015-10-05 07:22:17.122 LearnSpriteBuilder[10321:559370] 07:22:17.122 ERROR:     100: Error getting audio input device sample rate: '!obj'


看了一下,貌似是和音频输入设备有关,可是我是播放音乐,并没有录音啊,按道理只用到音频输出设备,不用输入设备吧?

然而同样的代码在MacAir和真机上都运行正常,声音播放的杠杠的.

于是到Stack Overflow搜索,发现2个比较靠谱的答案:

The error occurs because you are trying to get device's sample rate from simulator, which is not possible.

Because simulator is not a hardware. Its a software.

So some hardware functionality will not able to simulated with it.

It can not do the following:

Open Camera
Play sounds
Gyrometer
Accelerometer
Shake effect
And many more which required device hardware acceleration.

You need to test this in real device.


这哥们说模拟器是软件而不是硬件,无法播放音频,必须到真机上测试.

但是在macAir的模拟器上是可以播放的,所以问题不在这.

另一哥们说:

I was having this problem, the comment from @Matti Jokipii, helped me a lot.

You need to have a sound input enabled, so your Mac doesn't give you the null exception.
Go To System Preferences > Sound. And Check you have some input connected. If not, you will have to add a microfone, and re-rerun the app.




Ta说到系统设置的Sound中去看其中的Input栏中是否有设备,如果有那么可以播放,否则你必须添加一个输入设备才可播放.如果我没记错的话,MacAir是有输入设备的(可以录音),而Mac mini貌似没有.网上还有人说在mini上插个耳机就可以了,这应该是因为耳机上带有输入的话筒,不信可以到Input栏中看一下.但是没有输入话筒的耳机插上去也是不行的,我记得iPhone的耳机是带输入话筒的,而iPod shuffle耳机是没有的.

等回去再验证下.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息