如何停止音频SKAction?

MB_*_*per 11 ios7 sprite-kit skaction skview skscene

目标:我想呈现一个新场景:

[self.scene.view presentScene:level2 transition:reveal];
Run Code Online (Sandbox Code Playgroud)

并结束当前的背景音乐以启动新的背景音乐(2级的新背景音乐).

问题:在呈现新场景时,1.场景(level1)的背景音乐一直在播放,即使在离开迷你游戏时也不会停止,因为整个游戏由几个迷你游戏组成.

播放的音乐是SKAction:

@implementation WBMAnimalMiniGameLvL1

    -(id)initWithSize:(CGSize)size {    
        if (self = [super initWithSize:size])
        {
            /* Setup your scene here */
        self.temporaryScore = 0;
        self.animalSKSprites = [[WBMAnimalsMiniGameModel alloc] init];
        self.backgroundImage = [SKSpriteNode spriteNodeWithImageNamed:@"farmBackground1024x768.png"];
        self.backgroundImage.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));
        self.temporaryStartingPointIndex = -1;
        [self addChild:self.backgroundImage];
        self.playBackgroundSound = [SKAction playSoundFileNamed:@"farm.mp3" waitForCompletion:NO];

        //SKAction *repeat = [SKAction repeatActionForever:playSound];
        [self runAction:self.playBackgroundSound withKey:@"play"];

        [self drawAllAnimalsOntoScreen];

    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

这是向下一级别的过渡发生的:

-(void)transitionToNextLevel
{
    NSLog(@"transitionToNextLevel");
    SKTransition *reveal = [SKTransition moveInWithDirection:SKTransitionDirectionDown duration:0.5];
    //SKView *skView = (SKView *)self.view;
    SKScene *level2 = [[WBMAnimalMiniGameLvL2 alloc] initWithSize:self.size];

    level2.scaleMode = SKSceneScaleModeAspectFill;

    [self removeAllActions];
    [self removeActionForKey:@"play"];
    //self.scene.view.paused = YES;
    //self.playBackgroundSound = nil;
    [self.scene.view presentScene:level2 transition:reveal];
}
Run Code Online (Sandbox Code Playgroud)

在注释代码行中,您可以查看我已经尝试过的内容以及不起作用的内容.:

[self removeAllActions];
    [self removeActionForKey:@"play"];
Run Code Online (Sandbox Code Playgroud)

什么都没做.的:

self.scene.view.paused = YES;
Run Code Online (Sandbox Code Playgroud)

line仅停止转换,但音乐仍然继续.

我尝试过以下方法: - 使用弱或强属性:

@property (nonatomic,weak) SKAction *playBackgroundSound;
Run Code Online (Sandbox Code Playgroud)

保护指向SKAction的指针所以我可以使用"withKey"属性旁边访问它,因为我在"initWithSize"中初始化了SKAction.有人写道,SKAction是一个即发即弃的对象我理解为没有存储指针,以后访问它是不可能的(直接).但是,它没有工作/帮助我.

我查看了许多其他stackoverflow帖子,没有人帮助我或至少给了我一个线索为什么会发生这种情况:

SKAction playSoundFileNamed会停止背景音乐

停止使用RepeatsForever的SKAction - Sprite Kit

是否有可能结束SKAction中期行动?

暂停精灵套件场景

......思考:似乎动作是通过创建SKScene对象创建的.它被"迷住"并在音频持续时间之后完成.当我使用repeatForever时,它永远不会停止.但是没有办法暂停或停止它.我也尝试将SKAction挂钩到SKSpriteNode.装载SKScene时,动物会装载它.所以我尝试在SKSpriteNode上挂钩SKAction,并使用与SKSpriteNode相同的removeAllActions,但它也不起作用.

我检查了SKAction,SKView,SKScene,SKSpriteNode的文档,但最后它对我没什么帮助.

看起来该对象存在于运行其动作的某个地方.

错误不是关于:

  • 它不是模拟器或设备相关的错误 - 我在模拟器和设备上测试了它具有相同的结果(错误).

  • 这不是项目相关的错误 - 我在一个单独的项目中测试了它,它更小,结果相同(错误).

临时解决方案:我一直在使用AVFoundation框架中的AVAudioPlayer类.我创建了一个:

//@property (nonatomic) AVAudioPlayer *theBackgroundSong;
Run Code Online (Sandbox Code Playgroud)

这并没有多大帮助,因为我想在加载level2时更改theBackgroundSong并且我从嵌套的SKScene结构访问属性时遇到了很大的问题.

任何建议,线索,暗示或想法都会有很大帮助.

Dvo*_*ole 14

从SKAction运行时,您无法停止播放音频.您将需要另一个引擎来运行您的背景音乐.
试试AVAudioPlayer - 它非常简单易用.这样的东西会起作用:

首先导入标题并向场景或视图控制器添加新属性:

#import <AVFoundation/AVFoundation.h>

@interface AudioPlayerViewController : UIViewController {

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

}
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad之后或者在didMoveToView方法的场景中添加以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    self.audioPlayer.numberOfLoops = -1;

    if (!self.audioPlayer)
        NSLog([error localizedDescription]);                
    else 
        [self.audioPlayer play];

}
Run Code Online (Sandbox Code Playgroud)

每当您需要停止播放音乐时,只需拨打电话即可 [self.audioPlayer stop];

当您需要播放新音乐时,请将新播放器放入使用新乐曲初始化的属性中.

希望这可以帮助.

  • "从SKAction运行时,你无法停止播放音频" - 你是对的.我不确定,但现在我.它帮助:). (2认同)