First of all, the preferred sound format for iPhone is LE formatted CAF, or mp3 for music. You can convert a wav to caf with the built in terminal utility:
afconvert -f caff -d LEI16 crash.wav crash.caf OR directlyafconvert sound1.wav sound1.cafThen the easiest away to play a sound is to use the AVAudioPlayer... this quick function can help you load a sound resource:
- (AVAudioPlayer *) soundNamed:(NSString *)name {
NSString * path;
AVAudioPlayer * snd;
NSError * err;
path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL * url = [NSURL fileURLWithPath:path];
snd = [[[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&err] autorelease];
if (! snd) {
NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
} else {
[snd prepareToPlay];
}
} else {
NSLog(@"Sound file '%@' doesn't exist at '%@'", name, path);
}
return snd;
}
本文介绍如何将wav格式的音频文件转换为iPhone首选的CAF格式,并使用AVAudioPlayer进行播放。文中提供了使用afconvert工具进行格式转换的方法及一个用于加载声音资源并准备播放的示例函数。

被折叠的 条评论
为什么被折叠?



