如何在目标C iPhone编码播放声音 [英] How to play a sound in objective C iphone coding
问题描述
我有一个应用程序我的工作,它使用了一些硬件传感器来提供在屏幕上的数据,也就是用数字更新标签。我想要一个播放声音时的数量是超过100什么的。例如,假设它是阅读数字,那么所有的突然找到一个好去处(或其他),那么我想一个声音播放或一盏灯,照亮了。我是一个绝对的初学者,这将是很好,如果答案是容易让初学者绝对理解。
I have an app I am working on and It uses some of the hardware sensors to provide data on screen, there is a Label that updates with the number. I want a sound to play whenever the number is above 100 or something. For example, say it was reading numbers then all of the sudden it finds a good spot (or whatever), then I would like a sound to play or a light to light up. I am an absolute beginner and it would be nice if the answer would be easy for a absolute beginner to understand.
推荐答案
我使用的系统AudioToolbox.framework在我简单的玩游戏的声音。我说这个静态函数共同MyGame类:
I am using the system AudioToolbox.framework for playing sounds in my simple game. I added this static function to common MyGame class:
+ (SystemSoundID) createSoundID: (NSString*)name
{
NSString *path = [NSString stringWithFormat: @"%@/%@",
[[NSBundle mainBundle] resourcePath], name];
NSURL* filePath = [NSURL fileURLWithPath: path isDirectory: NO];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
return soundID;
}
我添加了Morse.aiff文件,项目资源和(任何)类的初始化有以下初始化它:
I added the "Morse.aiff" file to project Resources and initialized it in (any) class initialization with the following:
self.mySound = [MyGame createSoundID: @"Morse.aiff"];
然后我打的声音,这个调用:
And then I played sound with this call:
AudioServicesPlaySystemSound(mySound);
另外,不要忘记导入AudioServices.h文件。
Also, don't forget to import AudioServices.h file.
这声音工具箱也能起到不同的声音格式。
This audio toolbox can play also different sound formats.
这篇关于如何在目标C iPhone编码播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!