NSSound on the iphone

泪湿孤枕 提交于 2019-12-06 04:14:36

问题


I've been looking for a while how to play sound on the iphone, and I think it's something along the lines of:

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

But the NSSound is on the AppKit ... any suggestions ? I know there is a really simple answer to this, but today my searches are not rendering any result ...


回答1:


the new AVFoundation class is the easiest way to play sound on the iPhone, though it is for firmware 2.2 only:

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

you just need this import in the class that uses it:

#import <AVFoundation/AVAudioPlayer.h>



回答2:


Audio Toolbox system sounds are great for short async playback.

// 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);

I haven't gotten MP3s to work with AudioServicesPlaySystemSound. But it plays AIF files perfectly.



来源:https://stackoverflow.com/questions/388192/nssound-on-the-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!