为什么iOS系统声音无法在模拟器中播放? [英] Why doesn't ios system sound play in simulator?
问题描述
我正在尝试播放系统声音.使用Xcode 6 Beta4.这是代码:
I am trying to play a system sound. Using Xcode 6 Beta 4. Here is the code:
func playKeyClick()
{
let filePath = NSBundle.mainBundle().pathForResource("Tock", ofType: "aiff")
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
println("file exists")
}
let fileURL = NSURL(fileURLWithPath: filePath)
var soundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(fileURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
它可以在设备上正常运行,但不能在模拟器中播放.我创建了一个测试应用程序,除了播放此声音外什么也不做,所以我认为我没有做任何事情来阻止它.
It works fine on devices but does not play in the simulator. I have created a test app that does nothing but play this sound so I don't think I'm doing anything to stop it.
模拟器可以正常播放Safari应用程序中的声音,因此我认为它可以播放我的应用程序中的声音.
The simulator plays sound from the Safari app ok, so I assume that shows it is capable of playing sound from my app.
请注意,我还有其他代码可以通过两种其他方式播放声音,这些方式也可以在设备上运行,但不能在模拟器上运行.为了简单起见,我选择了此示例.
Note that I also have other code for playing sound in two other ways that also work on devices but not simulator. I chose this example for simplicity.
编辑-将文件更改为资源包以避免混淆
Edit - changed file to resource bundle to avoid confusion
推荐答案
我正在回答自己的问题,以澄清我所学到的东西.有三个问题使我的原始代码无法正常工作.注意,我在模拟器上使用的是Xcode6 Beta4和iphone 5s.
I am answering my own question to clarify what I have learned. There were three issues that prevented my original code from working. Note I'm using Xcode6 Beta4, and iphone 5s on the simulator.
1-我使用的"/System/Library/Audio/UISounds/Tone.caf"原始文件路径在模拟器中不起作用,它仅在设备上有效.通过将音频文件添加到应用程序来更正此问题.
1- The original file path I was using of "/System/Library/Audio/UISounds/Tone.caf" does not work in the simulator, it is only valid on the device. Corrected that by adding audio file to the app.
2- 声音在iPhone Simulator中不起作用?提供了以下信息:声音在ios 7.1及更早版本上正常工作.自从我在使用ios8进行测试时,不确定这些建议中的哪些可行.
2- Sound not working in iPhone Simulator? provided information that got sound working for ios 7.1 and earlier. Not sure which of the several suggestions worked since I was testing with ios8 as I ran through them.
3-因为在ios8上无法使用与ios7相同的代码,所以我假设io8模拟器代码中存在错误(ios8设备可以正常工作).在我尝试产生声音的三种方法中,只有一种无效:
3- Because the same code that works on ios7 doesn't work on ios8, I am assuming there is a bug in the io8 simulator code (ios8 device works OK). Out of three ways I have tried to produce sound, only one doesn't work:
AudioServicesPlaySystemSound()-不起作用
AudioServicesPlaySystemSound() - doesn't work
AudioUnit输出-有效
AudioUnit output - works
AVAudioPlayer-可以工作
AVAudioPlayer - works
这篇关于为什么iOS系统声音无法在模拟器中播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!