如何停止音频SKAction? [英] How to stop a audio SKAction?

查看:123
本文介绍了如何停止音频SKAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我想展示一个新场景:

Goal: I want to present a new scene:

[self.scene.view presentScene:level2 transition:reveal];

并结束当前的背景音乐以启动新的背景音乐(来自关卡的新背景音乐) 2)。

and end the current background music in order to start the new background music (the new background music from level 2).

TheProblem:在呈现新场景时,1.场景(level1)的背景音乐会继续播放,即使离开迷你游戏也不会停止,因为整个游戏包含几个迷你游戏。

TheProblem: Upon presenting the new scene the background music of the 1. scene (level1) keeps playing and DOES not stop even when leaving the miniGame since the whole game consists of few mini games.

播放的音乐是SKAction:

The music played is an 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;
}

以下是转换到下一个级别:

Here does the transition to the next level happen:

-(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];
}

在评论代码行中你可以看到我已经尝试过的东西以及做了什么不行。

In the comments code lines you can se what I have already tried and what did not work. The :

[self removeAllActions];
    [self removeActionForKey:@"play"];

绝对没有。

did absolutely nothing. The:

self.scene.view.paused = YES;

行仅停止转换,但音乐仍在继续。

line stops only the transition but the music still continues.

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

I have tried the following: - used a weak or strong property on a :

@property (nonatomic,weak) SKAction *playBackgroundSound;

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

to secure a pointer to the SKAction so I can access it beside using the "withKey" property since I initialized the SKAction in "initWithSize". Someone wrote that SKAction is a fire-and-forget object which I understood as without having stored a pointer to it accessing it later is not possible (directly). However, it didnt work/help me.

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

I looked into many other stackoverflow posts and none helped me or at least gave me a clue why this happens:

SKAction playSoundFileNamed停止背景音乐

停止使用RepeatsForever的SKAction - Sprite Kit

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

暂停精灵套件场景

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

... Thoughts: It seems that the action is created with the creation of the SKScene object. It is "hooked" to it and finished after the audio duration. While I was using repeatForever it would never stop. However there is no way to pause or stop it. I also tried hooking the SKAction to SKSpriteNode. When the SKScene is loaded animals are loaded with it. So I tried hooking the SKAction on the SKSpriteNode and use removeAllActions and alike from the SKSpriteNode but it didnt work aswell.

我检查了SKAction,SKView,SKScene,SKSpriteNode的文档但最终它并没有给我太多帮助。

I checked the documentation on SKAction,SKView,SKScene,SKSpriteNode but in the end it didnt help me much.

看起来这个对象存在于某处运行它的动作。

It looks like the object exists somewhere running its actions.

错误是什么:


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

  • It is not simulator or device related error - I have tested it on both the simulator and device with the same result (error).

这不是项目相关的错误 - 我在一个单独的项目中测试过它,小得多,并且与相同的结果(错误)。

It is not a project related error - I have tested it in a separate project , much smaller and the with the same result (error).

临时解决方案:
我一直在使用AVAudioPlayer类来自AVFoundation框架。我创建了一个:

A temporary solution: I have been using the AVAudioPlayer class from AVFoundation framework. I created a :

//@property (nonatomic) AVAudioPlayer *theBackgroundSong;

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

This didnt help much since I wanted to change theBackgroundSong when level2 was loaded and I had huge problems accessing the property from a nested SKScene structure.

任何建议,线索,提示或想法都会有很大的帮助。

Any advice,clue,hint or idea would be of great help.

推荐答案

从SKAction运行时,无法停止播放音频。
您将需要另一个引擎来运行您的背景音乐。

尝试AVAudioPlayer - 它非常简单易用。这样的东西将起作用:

You can't stop audio from playing when run from SKAction. You will need another engine to run your background music.
Try AVAudioPlayer - it is really easy and straightforward to use. Something like this will work:

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

First import header and add new property to your scene or view controller:

#import <AVFoundation/AVFoundation.h>

@interface AudioPlayerViewController : UIViewController {

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

}

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

After this in viewDidLoad or in didMoveToView method of scene add following code:

- (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];

}

每当您需要停止播放音乐时,只需致电 [self.audioPlayer stop];

Each time you need your music to stop, just call [self.audioPlayer stop];

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

When you need new music to play, put a new player into the property initialized with your new music piece.

希望这会有所帮助。

这篇关于如何停止音频SKAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆