我已经浏览了raywenderlich.com上的所有SpriteKit教程,并获得了除音频之外的每个教程中的所有内容。我无法在任何教程中使用任何声音效果。例如,在本教程http://www.raywenderlich.com/49697/sprite-kit-tutorial-making-a-universal-app-part-2中,我将声音文件复制到我的项目中,然后
1)将AVFoundation导入MyScene.h
#import <AVFoundation/AVFoundation.h>
2)在MyScene.h中添加了三个属性
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) SKAction *laughSound;
@property (strong, nonatomic) SKAction *owSound;
3)在MyScene.m
中的initWithSize中预加载声音效果// Add at the bottom of your initWithSize: method
// Preload whack sound effect
self.laughSound = [SKAction playSoundFileNamed:@"laugh.caf" waitForCompletion:NO];
self.owSound = [SKAction playSoundFileNamed:@"ow.caf" waitForCompletion:NO];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"whack" withExtension:@"caf"];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!self.audioPlayer) {
NSLog(@"Error creating player: %@", error);
}
[self.audioPlayer play];
4)用两种不同的方法将声音动作添加到序列中
// Add at bottom of popMole method, change the sequence action to:
SKAction *sequence = [SKAction sequence:@[easeMoveUp, setTappable, self.laughSound, self.laughAnimation, unsetTappable, easeMoveDown]];
// Add inside touchesBegan: method, change the sequence action to:
SKAction *sequence = [SKAction sequence:@[self.owSound, self.hitAnimation, easeMoveDown]];
当我在设备上运行代码时,有人可以解释为什么声音可能无法播放吗?请注意,当我制作常规iOS应用程序(没有SpriteKit)时,我能够毫无问题地使用AVFoundation。
更新
正如第一条评论中所建议的,我单独使用skaction运行声音文件,但它没有播放声音。 log语句每次都应该运行
SKAction *sound = [SKAction playSoundFileNamed:@"laugh.caf" waitForCompletion:NO];
NSLog(@"run sound");
[self runAction:sound];
答案 0 :(得分:0)
如果声音文件没有通过SKAction播放,但是同一个声音文件通过非精灵套件项目中的AVAudioPlayer播放,那么它几乎总是三件事之一:
如果没有看到项目的所有代码,我仍然打赌1或2.