我正在做一个IOS游戏,背景音乐片段有7秒长,所以当场景从一个场景切换到另一个场景时,我希望它立即停止,但是它继续并完成了7秒的循环,然后停止。这是代码:
[self runAction:[SKAction repeatActionForever:[SKAction playSoundFileNamed:@"sound 1.m4a" waitForCompletion:YES]]];
if (_dead == YES) {
[self removeAllActions];
}当玩家死的时候,_dead在哪里,新场景就会被触发。
我怎样才能让音乐停止那一瞬间,而不是完成它的循环呢?
发布于 2014-04-05 19:38:50
与使用SKAction播放背景声音文件不同,我建议使用允许您只发出stop命令的AVFoundation。
更新
我想不出任何食品教程主要集中在声音方面。大多数都是处理视频和声音。尝试谷歌的'AVFoundation教程‘之类的东西。
要使用AVFoundation,您可以为您的目的.
将AVFoundation.framework添加到项目中。
在.h文件中添加委托<AVAudioPlayerDelegate>
在您的.m文件#import <AVFoundation/AVFoundation.h>中
创建属性AVAudioPlayer *_backgroundMusicPlayer;
创建此方法:
- (void)playBackgroundMusic:(NSString *)filename {
NSError *error;
NSURL *backgroundMusicURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
_backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
_backgroundMusicPlayer.numberOfLoops = -1;
_backgroundMusicPlayer.volume = 0.2;
_backgroundMusicPlayer.delegate = self;
[_backgroundMusicPlayer prepareToPlay];
[_backgroundMusicPlayer play];
}开始播放[self playBackgroundMusic:[NSString stringWithFormat:@"bgMusic.mp3"]];
停止播放[_backgroundMusicPlayer stop];
这应该能起作用。我建议您阅读AVAudioPlayer类引用,以便了解它的属性。例如,设置为-1的numberOfLoops将无限期地循环声音,直到调用stop方法。另一个要记住的问题是音频文件的声音格式。AVFoundation对它将播放的声音文件有点挑剔。我总是坚持mp3。
模糊山羊也提出了一个很好的观点。我没有尝试他的声音,所以我不知道它是否会解决你的问题。它类似于你已经拥有的东西,但略有改变。
要启动声音:
SKAction *mySound = [SKAction repeatActionForever:[SKAction playSoundFileNamed:@"astroKitty 1.m4a" waitForCompletion:YES]];
[self runAction:mySound withKey:@"boogieDown"];为了阻止声音:
[self removeActionForKey:@"boogieDown"];https://stackoverflow.com/questions/22875934
复制相似问题