user3753628
user3753628

Reputation: 13

Playing sounds with xcode

I looked for my answer but I didn't get the answer that I looked for. I want to play sounds on my iphone project. Which file types I can use? How it's codes gonna be? I don't know anything about that and I need your help? Thanks in advence...

Upvotes: 0

Views: 89

Answers (1)

klcjr89
klcjr89

Reputation: 5902

Firstly in your ViewController.h, #import <AudioToolbox/AudioToolbox.h>

Then in your ViewController.m:

@property SystemSoundID theSound;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"Sound"
                                                    withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(soundURL), &_theSound);
}

- (IBAction)playSound
{
    AudioServicesPlaySystemSound(_theSound);
}

- (void)dealloc
{
    AudioServicesDisposeSystemSoundID(_theSound);
}

Upvotes: 1

Related Questions