简体   繁体   English

从SKAction播放音乐

[英]Playing music from a SKAction

I am trying to add background music to my game. 我正在尝试将背景音乐添加到我的游戏中。 I have a switch that will mute the music and when you tap it again it will play the music. 我有一个将音乐静音的开关,当您再次点击它时,它将播放音乐。 This is the code to play the music 这是播放音乐的代码

 var backgroundMusic = SKAction.playSoundFileNamed("Background.wav", waitForCompletion: true)
 runAction(SKAction.repeatActionForever(backgroundMusic), withKey: "music")

I know that the waitForCompletion should be set to false to stop the music when you tap the switch. 我知道应将waitForCompletion设置为false,以在您点击开关时停止音乐。 But when I have it set to false the music doesn't play it just is like the static sound. 但是,当我将其设置为false时,音乐就不会播放,就像静态声音一样。

self.removeActionForKey("music")

That is the code I used to stop the music. 那就是我用来停止音乐的代码。 I was wonder if you can mute the music until the track finished or it there is another way to play music forever in SpriteKit. 我想知道您是否可以将音乐静音直到曲目结束,还是在SpriteKit中有另一种永久播放音乐的方法。

waitForCompletion : If true , the duration of this action is the same as the length of the audio playback. waitForCompletion :如果为true ,则此action的持续时间与音频播放的长度相同。 If false , the action is considered to have completed immediately. 如果为false ,则认为该action已立即完成。

Setting waitForCompletion to false , will create an action with a duration of 0 . waitForCompletion设置为false将会创建一个持续时间为0的动作。 Thus the repeat forever action will not function properly. 因此,永远重复动作将无法正常运行。
You have to wait for the duration its playing before playing again from the start, for the music to loop. 您必须等待播放的持续时间,然后才能从头开始再次播放音乐。 So set waitForCompletion to true , 因此,将waitForCompletion设置为true

 var backgroundMusic = SKAction.playSoundFileNamed("Background.wav", waitForCompletion: true)

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKAction_Ref/index.html#//apple_ref/occ/clm/SKAction/playSoundFileNamed:waitForCompletion : https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKAction_Ref/index.html#//apple_ref/occ/clm/SKAction/playSoundFileNamed:waitForCompletion

You can stop the music by using removeActionForKey . 您可以使用removeActionForKey停止音乐。

I would recommend using ObjectAL instead of Spritekit for background music. 我建议使用ObjectAL代替Spritekit进行背景音乐。 Or here is a simple method I use for background music, borrowed from SKUtils : 或者这是我从SKUtils借来的用于背景音乐的简单方法:

-(AVAudioPlayer*)setupSound:(NSString*)file volume:(float)volume{
    NSError *error;
    NSURL *url = [[NSBundle mainBundle] URLForResource:file withExtension:nil];
    AVAudioPlayer *s = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    s.numberOfLoops = -1;
    s.volume = volume;
    [s prepareToPlay];
    return s;
}

Then you call it as follows: 然后,您将其称为如下:

...
AVAudioPlayer *bgplayer = [self setupSound:@"Background.wav" volume:1.0];
...
[bgplayer play];
...
[bgplayer pause];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM