我知道这个问题已经被问了999次了。所有答案都说转到"系统偏好设置">"声音>"音效",并确保选中"播放用户界面声音效果",或者取消选中并重新选中它。
这对我不起作用。 我已经检查了它,但音效仍然无法播放。
更多信息:
- 我正在尝试使用
SystemSoundID
来播放音效。 - 我以前用
AVAudioPlayer
来制作音效,它奏效了。
在 - 模拟器中从Safari上转到YouTube,视频声音确实播放了。
- 我已将
AudioToolbox.framework
链接到我的项目。 - 我从这里下载了苹果示例代码。
这也没有播放,所以我很确定我的代码没有问题。6. 我正在优胜美地上运行我的 Mac,OSx 10.10.1 和 XCode 6.1.1。
我还没有在实际设备上测试它,因为我目前没有开发人员的帐户。我不想支付我一生的积蓄99美元,只是为了发现有一个盒子是我没有选中的。
还有什么我应该尝试的吗?
我的代码:
音效.h
#import <AudioToolbox/AudioServices.h>
@interface SoundEffect : NSObject
{
SystemSoundID soundID;
}
- (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString*) extension;
- (void)play;
@end
音效
#import "SoundEffect.h"
@implementation SoundEffect
- (id)initWithSoundNamed:(NSString *)filename withExtension:(NSString *)extension
{
if ((self = [super init]))
{
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
if (fileURL != nil)
{
NSLog(@"fileURL found");
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError)
soundID = theSoundID;
}
}
return self;
}
- (void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
- (void)play
{
if([[NSUserDefaults standardUserDefaults] boolForKey:soundEffectsKey]) {
NSLog(@"Play sound effect");
AudioServicesPlaySystemSound(soundID);
}else {
NSLog(@"Sound effects are disabled");
}
}
注意:我创建了这个对象,并在项目中需要的任何位置播放它。当我尝试播放我的声音时,"找到文件URL"和"播放声音效果"都会被记录。
来源:在iOS中播放短音
SystemSoundID customSound;
NSString *soundFile = [[NSBundle mainBundle]pathForResource:<#customSoundName#> ofType:<#soundFormat#>];
NSURL *soundURL = [NSURL fileURLWithPath:openMenu];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, & customSound);
AudioServicesPlaySystemSound(customSound);
这是在我的身上工作