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

查看:18
本文介绍了如何停止音频 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).

问题:在出现新场景时,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"];

什么都没做.该:

self.scene.view.paused = YES;

line 只停止过渡,但音乐仍在继续.

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 停止背景音乐

停止 SKAction that RepeatsForever - Sprite Kit

是否可以结束 SKAction 中间动作?

暂停精灵套件场景

...感想:看来动作是随着SKScene对象的创建而创建的.它与它挂钩"并在音频持续时间后完成.当我使用 repeatForever 时,它永远不会停止.但是没有办法暂停或停止它.我还尝试将 SKAction 挂钩到 SKSpriteNode.当 SKScene 加载时,动物就会加载它.所以我尝试在 SKSpriteNode 上挂钩 SKAction 并使用 removeAllActions 和来自 SKSpriteNode 的类似方法,但它不起作用.

... 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).

临时解决办法:我一直在使用来自 AVFoundation 框架的 AVAudioPlayer 类.我创建了一个:

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

//@property (nonatomic) AVAudioPlayer *theBackgroundSong;

这没有多大帮助,因为我想在加载 level2 时更改背景歌曲,并且从嵌套的 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天全站免登陆