尝试使用 SimpleAudioEngine 播放随机声音,没有声音

Trying to play a Random Sound using SimpleAudioEngine, no sound

所以最初我要制作的应用程序只在向上滑动时发出一种声音。代码是

[[SimpleAudioEngine sharedEngine] playEffect:@"Swoosh.mp3"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:@selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;

我决定在每次滑动时随机播放三种声音,而不是只有一种。这就是我所做的。

NSString *Soundname;

int randSound = arc4random() % 3;
switch (randSound) {
    case 0:
        Soundname = @"Swoosh1.mp3";
        break;
    case 1:
        Soundname = @"Swoosh2.mp3";
        break;
    case 2:
        Soundname = @"Swoosh3.mp3";
        break;

}
[[SimpleAudioEngine sharedEngine] playEffect:@"Soundname"];
CCMoveTo *moveto=[CCMoveTo actionWithDuration:0.50 position:ccp(currentSkewed.position.x, [UIScreen mainScreen].bounds.size.height+300)];
CCScaleTo *scalto=[CCScaleTo actionWithDuration:0.30 scale:0.5];
CCSequence *seq=[CCSequence actionOne:moveto two:[CCCallFuncN actionWithTarget:self selector:@selector(RemoveSprite:)]];
[currentSkewed runAction:seq];
[currentSkewed runAction:scalto];
[skewdArray removeObject:currentSkewed];
currentSkewed=nil;

我制作了字符串,然后替换了playEffect:@"Swoosh.mp3"];与 playEffect:@"Soundname"];

我尝试了 Soundname 的多种变体,none 在我看来很有效。我在编码方面是新手,我知道这是我所缺少的愚蠢的东西。有人有什么想法吗?

我很确定问题是因为您仍在为 playEffect 参数使用文字字符串,但您的意图是使用新定义的字符串变量。

尝试更改:

[[SimpleAudioEngine sharedEngine] playEffect:@"Soundname"];

对此(注意缺少引号):

[[SimpleAudioEngine sharedEngine] playEffect:Soundname];