views:

391

answers:

3
sound = [[NSSound alloc] initWithContentsOfFile:@"staticbeam09.wav" byReference:YES];

Code referenced from Apple docs. Getting an error when I put this in viewDidLoad. If I put

NSSound *sound;

in the header file, I get the specifier-qualifier error at the top of my implementation file. What do I have to do to make this work? I was just pasting the code from Apple's documentation - has this been deprecated? Thanks for your help!

+3  A: 

There is no NSSound class in iPhone SDK It's only for MacOS

oxigen
+3  A: 

Oxigen’s right. If you want to play sounds on iPhone, there are several options. Probably the easiest one is using Audio Services (example wrapper), then there is AVAudioPlayer and then you can also use OpenAL (I’ve written a very basic OpenAL sound effect engine called Finch). Depends on what you need to do.

zoul
Thanks for the replies. Zoul, I tried putting your code in my main implementation file, and in viewDidLoad I put: Sound *abc = [[Sound alloc] initWithPath:[NSString stringWithFormat:@"staticbeam09.wav"]]; [abc play];It compiles fine, but I get an uncaught exception at runtime. Any idea why? Thank you both for your help.
quantumpotato
The `stringWithFormat:` call is not necessary, you can use `@"string"` directly. The code is not exactly what I use, I might have left some bug in there. What exception do you get? Do you have the sound file added in the Resources group in Xcode?
zoul
Oh, I looked at the view report and it said there was an error in loading the sound. Double-checked the names and their fine, double-clicking the file in Resources plays it in Itunes too.
quantumpotato
Sorry, in that case you’ll have to debug the code yourself. Take a look inside the `initWithPath:` method, see what `resourceDir` and `fullPath` expand to and check if the file is really on that path. This has to work, there’s just a tiny mistake somewhere.
zoul
A: 

http://icodeblog.com/2009/05/04/iphone-game-programming-tutorial-part-4-basic-game-audio/

that is how i did the audio bg music and sound effects at the end for iconquer

Sj
Hey, tried that. I'm getting a nil string parameter crash on the second line of this: NSString *beamPath = [[NSBundle mainBundle] pathForResource:@"staticbeam09" ofType:@"wav"]; CFURLRef beamURL = (CFURLRef ) [NSURL fileURLWithPath:beamPath];staticbeam09.wave is in my resources folder. tried putting \resources\staticbeam09 and that didn't work.. any idea what parameter it thinks I'm passing as nil?
quantumpotato
I don't have my code infront of me but from a glance it looks like the resource you are using needs to be in caf format, the iPhone doesn't utilize a windows audio codec. Icodeblog includes a terminal command for converting it. Try that and lemme kno
Sj
Oh! I thought .wav was a universal lossless format. It works now! Thanks to everyone who helped out on this. Now I've got sounds working in my game!
quantumpotato
Well, one sound does. I used Itunes to convert to m4a, the other sound I converted and nothing plays, also tried playing an MP3 and I hear no sound.. no errors, and I can play the working sound just fine.. each sound file has their own SystemSoundID etc. Could there be some setting/attribute of the sound file I can check that would give reason for Xcode to not play it? I hope this isn't too vauge.. I'll try other sounds, seems weird one will work and the others won't.
quantumpotato
(and yes each one has their own url / path variable)
quantumpotato
Hm.. even that static sound won't play on my iPod, but it plays in the Simulator. Is there something else I have to do? Thanks again everyone for all of your help!
quantumpotato
In my experience, I have only ever been able to use the .caf format, it may be that that is the only one supported by the framework. I have 3 different .cafs in my game and they all work just fine, if you need anymore I can upload the code as I used it.
Sj
Sorry for the extra comments, my mistake! converted to m4a not realizing I had to change Itunes to get AIFF, then I switched that to .CAF. MP3's still not playing but the .wav to .caf's work, I'll figure out the mp3. Thanks again.
quantumpotato
no prob, i was able to convert mp3 using that command from before (atleast thats what i think it was originally)
Sj