我在游戏中遇到了背景音乐问题。当我从主菜单场景切换到游戏场景时停止,但是当游戏场景切换到gameOver场景时不停止。当我选择重放时(从gameOver场景切换回游戏场景),音乐也会重叠。 要播放音乐我使用AVAudioPlayer。 ([玩家停止],[玩家玩]等:)) 这里似乎是什么问题,我如何让音乐连续播放?
答案 0 :(得分:1)
我个人使用Ray Wenderlich的音频库,可在此处找到: https://github.com/raywenderlich/SKTUtils/tree/master/SKTUtils
你只需要SKTAudio.h& .M 但其他图书馆也非常好
启动音乐: [[SKTAudio sharedInstance] playBackgroundMusic:@" bgMusic.mp3"]; 暂停: [[SKTAudio sharedInstance] pauseBackgroundMusic];
答案 1 :(得分:-1)
理想情况下,你应该有一个单独的方法来设置你的声音,然后用另一种方法播放它们。
首先,设置您的属性:
@property (nonatomic) AVAudioPlayer *backgroundMusic;
@property (nonatomic) AVAudioPlayer *gameOverMusic;
在initWithSize中:
-(id)initWithSize:(CGSize)size {
...Your other code goes here
[self setUpSounds];
}
然后声明您设置音乐的方法:
-(void) setupSounds {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// -1 creates an infinite loop
player.numberOfLoops = -1;
//Not [player play];
[player prepareToPlay];
NSURL *gameOverURL = [[NSBundle mainBundle] URLForResource:@"GameOver" withExtension:@"mp3"];
self.gameOverMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:gameOverURL error:nil];
// 1 only plays once
self.gameOverMusic.numberOfLoops = 1;
[self.gameOverMusic prepareToPlay];
}
播放背景音乐:
- (void) didMoveToView:(SKView *)view {
[self.backgroundMusic play];
}
GameOver屏幕的另一种方法:
- (void) performGameOver {
...Your other code here
// This stops the background music and plays gameOverMusic so they won't clash
[self.backgroundMusic stop];
[self.gameOverMusic play];
}