NSSound在iPhone上

web*_*ber 4 iphone cocoa-touch core-audio

我一直在寻找一段时间如何在iphone上播放声音,我认为这有点像:

[[NSSound soundNamed:@"cat.mp3"] play];
Run Code Online (Sandbox Code Playgroud)

但NSSound是在AppKit上...任何建议?我知道有一个非常简单的答案,但今天我的搜索没有呈现任何结果......

rus*_*elf 10

新的AVFoundation类是在iPhone上播放声音的最简单方法,尽管它仅用于固件2.2:

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
[audioPlayer play]; 
Run Code Online (Sandbox Code Playgroud)

你只需要在使用它的类中导入:

#import <AVFoundation/AVAudioPlayer.h>
Run Code Online (Sandbox Code Playgroud)


Fra*_*ger 7

Audio Toolbox系统声音非常适合短暂的异步播放.

// 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);
Run Code Online (Sandbox Code Playgroud)

我还没有使用MP3 AudioServicesPlaySystemSound.但它完美地播放AIF文件.