1

I am trying to make a background music in my game with AVAudioPlayer, so I set this code at the initWithSize method of my SKScene:

//SETTING BACKGROUND MUSIC//
NSError * err = nil;
NSURL * musicFile = [[NSBundle mainBundle] URLForResource:@"Cosmic Rush" withExtension:@"m4a"];
music = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:&err];
if(err)
{
    NSLog(@"%@", [err userInfo]);
    return;
}
[music prepareToPlay];
music.numberOfLoops = -1;
[music setVolume:0.5];
//SETTING BACKGROUND MUSIC//

Then, in another method: [music play]; However, the sound never plays, and I have tried all sort of extensions: .mp3, .caf, .mp4 etc. I have also checked people who had the same problem, but none of the solutions that solved their problems worked with mine.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
lucas_turci
  • 315
  • 1
  • 3
  • 12
  • Are you getting any error logged using the NSLog? Have you tried setting breakpoints? – Rafał Sroka Apr 30 '14 at 23:22
  • Couple of questions: what is the exact name of your audio file (as it appears in your project)? Have you tried not having a space in your audio file name? Have you tried URLForResource:@"SongName.mp3" withExtension:nil]; instead of what you currently have (modify mp3 to the actual format)? Have you set the AVAudioPlayer delegate? Are you able to play other audio file or just not this one? – sangony May 01 '14 at 00:20
  • Ok, I tried without space in the file name, with the mp3 extension, and now an error appeared in the NSLog box, but with nothing inside it. But I haven't set an AVAudioPlayer delegate. How do I do that? – lucas_turci May 01 '14 at 00:35
  • 1
    Consider making the ivar named music in your code a property with a strong reference. See this SO post for more info ->[link](http://stackoverflow.com/questions/10856332/avaudioplayer-no-sound) – Bamsworld May 01 '14 at 03:16
  • Try logging this: NSLog(@"Error occurred: %@", [err localizedDescription]); – Theis Egeberg May 01 '14 at 07:00
  • Ok, I tried making a strong reference to the music var, but anything changed. I used "localized description" and I got this: The operation couldn’t be completed. (OSStatus error 1685348671.) – lucas_turci May 01 '14 at 11:58
  • I solved the problem... The audio file went to the wrong location after I did "drag and drop" to my project in Xcode. I appreciate all the answers anyway – lucas_turci May 01 '14 at 18:32

1 Answers1

0

there is a easier way to add background music to your scene. i use the following steps and works just as good as any other methods and it is easier.

import the @import AVFoundation; at the top of your scene.

add it as a property right after your @implementation.

AVAudioPlayer *_backgroundMusicPlayer;

then declare a method for the music

- (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 prepareToPlay];
    [_backgroundMusicPlayer play];
}

after that create a reference to that method in your -(id)initWithSize:(CGSize)size like the following

[self playBackgroundMusic:@"bgMusic.mp3"];

that should take care of the background music for you.

Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • thank you for your answer, but I found out that the problem was something about referencing my audio file. It was in the wrong location. – lucas_turci May 01 '14 at 18:30