Reputation: 958
I used this code to loop a audio file:
// *** 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];
}
But when the scene is changed, the audio stops. Is there a way to keep it playing in the background during all scenes?
Upvotes: 3
Views: 1844
Reputation: 76
To add to this. Here's is the SpriteKit Swift 3.0 version of @user3504848 self.run(SKAction.playSoundFileNamed("YourFileName.extension", waitForCompletion: false))
Upvotes: 0
Reputation: 39
Go to your main SKScene and add this:
[self runAction:[SKAction playSoundFileNamed:@"YourFileName.extension" waitForCompletion:NO]];
It will keep playing forever, even when you transition scenes.
Peace.
Upvotes: 2
Reputation: 26223
You will have set the audio player up at a level above the SKScene, I did this in one of my Sprite Kit apps by having the UIViewController play the music, then when I swapped SKScenes the music continued to play.
Upvotes: 3