我一直在寻找了一段时间如何在iPhone上播放声音,我认为这是沿着线的东西:

[[NSSound soundNamed:@"cat.mp3"] play];

但NSSound是上了AppKit ...有什么建议?我知道有一个非常简单的答案,但我今天的搜索都没有渲染结果...

有帮助吗?

解决方案

新AVFoundation类播放声音在iPhone上最简单的方法,但它是2.2固件只:

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
[audioPlayer play]; 

您只需要这个进口类中的使用它:

#import <AVFoundation/AVAudioPlayer.h>

其他提示

音频工具箱系统声音非常适合短异步播放。

// Initialize
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Init each sound
CFURLRef      tapURL;
SystemSoundID tapSound;
tapURL = CFBundleCopyResourceURL(mainBundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(tapURL, &tapSound);

// Play the sound async
AudioServicesPlaySystemSound(tapSound);

我还没有得到的MP3与AudioServicesPlaySystemSound工作。但是它扮演AIF文件完美。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top