我使用这段代码来循环播放音频文件:
// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>
...
AVAudioPlayer *testAudioPlayer;
// *** Implementation... ***
// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;
// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
if(audioError != nil) {
NSLog(@"An audio error occurred: \"%@\"", audioError);
}
else {
[testAudioPlayer setNumberOfLoops: -1];
[testAudioPlayer play];
}
但是当场景改变时,音频停止了。有没有办法让它在所有场景中都在后台播放?
最佳答案
您将在 SKScene 之上的级别设置音频播放器,我在我的一个 Sprite Kit 应用程序中通过让 UIViewController 播放音乐来做到这一点,然后当我交换 SKScenes 时音乐继续播放。
关于ios - Sprite Kit - 改变场景时继续播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107219/