3

I have would like to play audio on the startup of my app, but, first of all, the audio doesn't play! No matter how i code it. And secondly I would love to loop it if i can.

@implementation ViewController

-(void)awakeFromNib
{
    SystemSoundID soundID;
    NSString *soundFile1 = [[NSBundle mainBundle] pathForResource:@"ScaryMusic" ofType:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef) [NSURL fileURLWithPath:soundFile1], &soundID);

    AudioServicesPlaySystemSound(soundID);
    NSLog(@"Sound Played");

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Brandon Boynton
  • 115
  • 1
  • 9

2 Answers2

4

Try this instead:

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *player;
NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"ScaryMusic" withExtension:@"mp3"];
if (soundFileURL != nil)
{
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
    player.numberOfLoops = -1; //infinite
}

[player play];

This is better for mp3's and such. SystemSound is mostly reserved for short .wav type sounds.

Aeramor
  • 330
  • 2
  • 10
  • 1
    Thank you! :D But can i play several sounds at the same time with this function? It doesn't seem to be letting me.. :/ – Brandon Boynton Jul 12 '13 at 17:31
  • Yes you can but you need a new player for each sound. This answer should help for multiple sounds http://stackoverflow.com/a/11195226/1550927 – Aeramor Jul 12 '13 at 18:24
  • If this worked for you accept the answer so others can see it easily. – Aeramor Jul 16 '13 at 21:22
0

If you look in the docs you'll find that mp3 is not among the formats supported by AudioToolbox

KHansenSF
  • 604
  • 4
  • 7