在iPhone上播放声音样本

时间:2023-01-24 08:35:47

I've reached the point where i want to play some samples in my game/app. My instinct says use openAL ... I will have the situation where I will need to play multiple samples at once (however no more than 2 or 3) and the samples will be short (2 or 3 seconds).

我已经达到了想要在我的游戏/应用程序中播放一些样本的程度。我的直觉说使用openAL ...我会遇到需要一次播放多个样本(但不超过2或3)的情况,样本会很短(2或3秒)。

My question is what is the best way of playing samples on the iPhone given that criteria?

我的问题是,根据这个标准,在iPhone上播放样本的最佳方式是什么?

1 个解决方案

#1


This is a piece of sample code I am using:

这是我正在使用的一段示例代码:

1st option:

I defined this in my .h file

我在.h文件中定义了这个

SystemSoundID topClick;

And in my .m file I firest load the sound (aiff file):

在我的.m文件中,我会加载声音(aiff文件):

NSBundle* bundle = [NSBundle mainBundle];
NSString *topClickFile = [bundle pathForResource:@"top_click" ofType:@"aiff"];

Then, when I want to play the sound, I use:

然后,当我想播放声音时,我使用:

AudioServicesPlaySystemSound(topClick);

2nd option:

You could also use the AVAudioPlayer (available since firmware 2.2 I think):

您也可以使用AVAudioPlayer(我认为固件2.2之后可用):

NSString *graffitiSprayFile = [bundle pathForResource:@"sound_effect" ofType:@"aiff"];
AVAudioPlayer* sprayAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:graffitiSprayFile] error:NULL];
sprayAudio.delegate = self;
[sprayAudio prepareToPlay];
[sprayAudio setNumberOfLoops:999];

The first option is very usable for relatively short sound effect.

第一个选项非常适用于相对较短的音效。

#1


This is a piece of sample code I am using:

这是我正在使用的一段示例代码:

1st option:

I defined this in my .h file

我在.h文件中定义了这个

SystemSoundID topClick;

And in my .m file I firest load the sound (aiff file):

在我的.m文件中,我会加载声音(aiff文件):

NSBundle* bundle = [NSBundle mainBundle];
NSString *topClickFile = [bundle pathForResource:@"top_click" ofType:@"aiff"];

Then, when I want to play the sound, I use:

然后,当我想播放声音时,我使用:

AudioServicesPlaySystemSound(topClick);

2nd option:

You could also use the AVAudioPlayer (available since firmware 2.2 I think):

您也可以使用AVAudioPlayer(我认为固件2.2之后可用):

NSString *graffitiSprayFile = [bundle pathForResource:@"sound_effect" ofType:@"aiff"];
AVAudioPlayer* sprayAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:graffitiSprayFile] error:NULL];
sprayAudio.delegate = self;
[sprayAudio prepareToPlay];
[sprayAudio setNumberOfLoops:999];

The first option is very usable for relatively short sound effect.

第一个选项非常适用于相对较短的音效。