【发布时间】:2012-04-08 07:20:19
【问题描述】:
我更新到 OS X Lion 10.7.3 和 Xcode 4.3.2。我无法使用以下代码在 iPhone 模拟器或 iPhone 设备上播放声音。我可以在 Snow Leopard 和 Xcode 4.2 上使用相同的代码播放声音。我不知道为什么我无法听到 4.3.2 中的声音。有人可以帮忙吗?
.h 文件
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface soundViewController : UIViewController {
}
-(IBAction)playSounds:(id)sender;
@end
.m 文件
-(IBAction)playSounds:(id)sender {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"E", CFSTR("caf"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
【问题讨论】:
-
是
soundFileURLRefNULL?您是否将E.caf添加到目标的资源中? -
我把 NSError *error NSLog(@"%@", error, userInfo) 和它都返回 null 但在 iMac 上播放 E.caf 在我的目标资源中。
-
这不是您检查错误的方式。 NSError 不会突然出现,它需要通过某种方法返回。您调用的 CF 或 AudioServices 函数都不会返回 NSError。
标签: iphone ios xcode macos audio