How do you play sounds on the iPhone?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi friends!

I would like to know your opinions.
I'm using C++ and OpenGL (minimun APPLE's API), what should I use to play sounds? Do you know any sample/library or source code?
I know there is a sample, but it is said it has bugs. FMOD cost 500 $ which a bit expensive for a small project.

Any suggestion?

Thanks a lot Smile
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
I'd recommend OpenAL. If I'm not mistaken, the iPhone comes with an implementation of it.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #3
Depending on what you want to play, there are much easier solutions than OpenAL.

If you want to play a sound <30seconds in duration and in .caf, .aiff, .wav format, use AudioService.

Code:
#import <AudioToolbox/AudioToolbox.h>

//creating it
SystemSoundID shortSound;
NSURL* audioFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                    pathForResource:@"photoShutter"
                                                             ofType:@"aiff"]];
AudioServicesCreateSystemSoundID((CFURLRef)audioFile, &shortSound);

//playing it
AudioServicesPlaySystemSound(shortSound);

//cleanup
AudioServicesDisposeSystemSoundID(shortSound);

If you want to play a more "complicated" audio format, such as mp3, or it is longer than 30 seconds in duration, use the new AVAudioPlayer.
Code:
#import <AVFoundation/AVFoundation.h>

//Creating it
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                    pathForResource:@"Music"
                                                             ofType:@"mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile
                                                         error:nil];
//Playing it
[audioPlayer play];
Quote this message in a reply
Moderator
Posts: 384
Joined: 2002.08
Post: #4
AVAudioPlayer, new to iPhone OS 2.2, is a fairly easy way to play audio. I recommend that.

KB Productions, Car Care for iPhone/iPod Touch
@karlbecker_com
All too often, art is simply the loss of practicality.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #5
Thanks a lot for replies!

I would like to add sound to my game, I mean, sound FX and ambient music. I guess format should be MP3 (but I'm not sure)

So, is AVAudioPlayer part of the iPhone API? Is a bad idea play mp3 with this an at the same time real time 3d graphics? Can I use AVAudioPlayer on C++ or should I create a wrapper?

Thanks friends!
Quote this message in a reply
Member
Posts: 21
Joined: 2008.10
Post: #6
What about if I wanted to play sounds at different speeds, like a dj scratching a record?
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.04
Post: #7
longjumper Wrote:Depending on what you want to play, there are much easier solutions than OpenAL.

If you want to play a sound <30seconds in duration and in .caf, .aiff, .wav format, use AudioService.

Code:
#import <AudioToolbox/AudioToolbox.h>

//creating it
SystemSoundID shortSound;
NSURL* audioFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                    pathForResource:@"photoShutter"
                                                             ofType:@"aiff"]];
AudioServicesCreateSystemSoundID((CFURLRef)audioFile, &shortSound);

//playing it
AudioServicesPlaySystemSound(shortSound);

//cleanup
AudioServicesDisposeSystemSoundID(shortSound);

If you want to play a more "complicated" audio format, such as mp3, or it is longer than 30 seconds in duration, use the new AVAudioPlayer.
Code:
#import <AVFoundation/AVFoundation.h>

//Creating it
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                    pathForResource:@"Music"
                                                             ofType:@"mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile
                                                         error:nil];
//Playing it
[audioPlayer play];





I am trying to play multiple files (sound1.wav, sound2.wav, sound3.wav, etc...)
after a button is pushed and have used your code in a loop and tried copying it three times, but it simply will not play one file after the other. Do I need to queue the sound files? I also noticed a method called: AudioServicesAddSystemSoundCompletion but am not sure how to implement it.

PLEASE help I have been struggling for days to get my program to play a few files one after the other.

Thank you so much.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Playing sounds and music on iPhone, which API to use? Jamie W 17 9,238 Apr 20, 2010 07:55 AM
Last Post: Beast
  how to play short sounds asynchronously ishaq 1 3,155 Sep 11, 2008 10:03 PM
Last Post: AnotherJake