如何使用SKAction循环播放音乐? [英] How to loop music with SKAction?

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

问题描述

我想用SKAction循环播放背景音乐,但是当我切换到另一个场景时,音乐会在一行之后停止播放.有没有办法开始循环并继续在不同的场景中播放?

I want to loop my background music with an SKAction but the music stops after one row when I switch to an other scene. Is there a way to start the loop and keep playing it over different scenes?

现在,将代码放置在MyScene的init方法中-这是正确的位置吗?也许FinishLaunchingWithOptions吗?

Right now the code is placed in the init method of MyScene - is that the correct place? Maybe didFinishLaunchingWithOptions?

这是我尝试过的:

if (delegate.musicOn == YES && delegate.musicIsPlaying == NO) {

    SKAction *playMusic = [SKAction playSoundFileNamed:@"loop.wav" waitForCompletion:YES];
    SKAction *loopMusic = [SKAction repeatActionForever:playMusic];
    [self runAction:loopMusic];

    delegate.musicIsPlaying = YES;


}

推荐答案

使用SKAction不能在场景中播放背景音乐.使用AVAudioPlayer代替:

You can't play background music over the scenes with using SKActions. Use AVAudioPlayer instead:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSError *error;
AVAudioPlayer *gameSceneLoop = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:&error];
if (error) {
    NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
} else {
    gameSceneLoop.numberOfLoops = -1;
    [gameSceneLoop prepareToPlay];
    [gameSceneLoop play];
}

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

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