发挥CAF声音在iPhone上最简单的方法? [英] Easiest way to play a CAF sound on the iPhone?

查看:289
本文介绍了发挥CAF声音在iPhone上最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多难做到这一点?有人告诉我,这是对iPhone令人难以置信的硬播放音频。真的不能相信。但他说,玩的是辛苦,但在同一时间玩两个或三个是不可思议的辛苦。听起来吓人。什么你们可以说这个?

How hard is it to do that? Someone told me that it's incredible hard playing audio on the iPhone. Can't really believe it. But he said playing one is hard, but playing two or three at same time is incredible hard. Sounded scary. What can you guys say about this?

我想要做的:我有一点光点和蜂鸣声和按钮preSS听起来约10 CAF文件。没有什么特别。现在我要播放这些文件每次有事。和几件事情可能发生在同一时间。因此,它可能发生的播放重叠。

What I want to do: I have about 10 CAF files with little blips and beeps and button press sounds. Nothing really special. Now I want to play these files every time something happens. And several things could happen at same time. So it could happen that playbacks overlap.

也许有一个框架,我可以利用这样做?

Maybe there's a framework I can use for doing this?

推荐答案

打小光点,发出蜂鸣声,等(声音小于30秒),实际上是pretty容易 - 这正是AudioServices的意思做。您需要首先获取每个声音的ID,使用这样的:

Playing small blips, beeps, etc (sounds less than 30 seconds) is actually pretty easy - this is exactly what AudioServices is meant to do. You need to first obtain an ID for each sound, using something like this:

SystemSoundID soundID = 0;
CFURLRef soundFileURL = (CFURLRef)[NSURL URLWithString:somePathString];
OSStatus errorCode = AudioServicesCreateSystemSoundID(soundFileURL, &soundID);
if (errorCode != 0) {
  // Handle failure here
}

您会再抱到该ID,直到你需要使用它,那么你可以简单地使用这条线播放声音:

You'll then hold onto that ID until you need to use it, and then you can play the sound simply using this line:

AudioServicesPlaySystemSound(soundID);

还有 AudioServicesPlayAlertSound()将表现略有不同取决于设备的能力,例如,如果该设备被用户设置为振动的iPhone。您应该阅读<一href=\"http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html\"相对=nofollow>苹果在文档说明一个更完整的解释。

There's also AudioServicesPlayAlertSound() that will behave slightly differently depending on the device's capability, such as vibrating an iPhone if the device is configured to by the user. You should read Apple's notes in the docs for a more complete explanation.

请注意,您对使用AudioServices声音的回放最低程度的控制 - 你不能阻止他们,控制音量等AudioServices只是针对警报声音和其他类似的短哔声和光点。还有就是,当然,AVAudioPlayer但它比你在这种情况下所需要的更多的重量级选手。你可能会更好过与AudioServices坚持,除非它的制约因素之一,使之无法使用。

Note that you have minimal control over the playback of sounds using AudioServices - you can't stop them, control the volume, etc. AudioServices is only meant for alert sounds and other such short beeps and blips. There is, of course, AVAudioPlayer but its a bit more heavyweight than what you need in this case. You're probably better off sticking with AudioServices unless one of its constraints makes it unusable.

您需要才能使用这些功能链接AudioToolbox.framework。

You need to link AudioToolbox.framework in order to use these functions.

P.S。欢迎堆栈溢出!不要忘记阅读常见问题解答和接受标记为解答您的问题(如果他们是很好的答案值得接受,当然)。

P.S. Welcome to Stack Overflow! Don't forget to read the FAQ and mark accepted answers for your questions (if they're good answers worth accepting, of course).

这篇关于发挥CAF声音在iPhone上最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆