Nicholas Whitley
Nicholas Whitley

Reputation: 81

music not looping with AVAudioPlayer

In my "initWithSize" in my main menu implementation file I put in code to play music. I have a loading screen pop up and then the main menu scene loads in. When the loading screen pops up it starts playing music but the loading screen never goes away and I want the music to loop and it's not looping. I don't really know what's going on here.

    NSError *error;
    NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"POL-parallel-fields-short" withExtension:@"mp3"];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
    [player setVolume:0.1];
    [player prepareToPlay];

    SKAction*   playAction = [SKAction runBlock:^{
        [player play];
        }];
    SKAction *playMusic = [SKAction repeatActionForever:playAction];

[self runAction:playMusic];

Upvotes: 0

Views: 274

Answers (1)

AMI289
AMI289

Reputation: 1108

I don't think that your method of 'looping' is correct...

You don't need to create an action for that,
Just try changing your code to this-

NSError *error;  
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"POL-parallel-fields-short" withExtension:@"mp3"];  
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];  
[player setVolume:0.1];  
[player prepareToPlay];  
player.numberOfLoops = -1;  
[player play];

numberOfLoops is a property that set the number of loops the audio will play.
From apple's documentation- 0 is the default, that will play the audio once.
Any positive number will set the number of times it'll be played.
Any negative number will cause the music to loop indefinitely, until stop is explicitly called.

I have no idea why your loading screen is not going away,
Since you haven't included any code that relates to it.

But the above will solve your music issue mate.

Upvotes: 0

Related Questions