New Sound Manager Version

If you have followed the tutorials you will know that tutorial 9 covered the sound manager classes. Since the tutorial I’ve been making updates to those classes so that they correctly handle sound interruptions, such as alarms or phone calls. There are also a number of other bug fixes that I’ve added as well to areas such as the removal of sounds when they are no longer needed.

This is the same version of the sound manager that is being use in Sir Lamorak’s Quest and its been performing really well. Hope it helps.

New SoundManager Classes

Mike

Share:
  • Digg
  • del.icio.us
  • Facebook
  • MySpace
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Design Float
  • DZone
  • email
  • Google Bookmarks
  • LinkedIn
  • Scoopeo
  • Tumblr

27 Comments

RoberRM  on January 10th, 2010

Thank you for sharing it, mike!

Simon  on January 10th, 2010

Thanks Mike looking forward to checking this out

Eugene  on January 10th, 2010

Thanks, Mike. Will try it.

Eugene  on January 10th, 2010

Just checked it.
Interface is modified. So I couldn’t make it work properly just by copy and paste.
I’ve already found a couple of problems.
Mike, you’ve not included Global.h. Class uses GLfloat which is not defined. Maybe it is defined in Global.h. Inside SoundManager you can find SLQLOG which is not defined too.

mike  on January 10th, 2010

Hi Eugene

Thanks for the feedback. Stupid mistakes on my part leaving in the items that are not defined in the classes themselves.

I’ll make changes to the class so that it is not using anything externally to it.

I’ll get it up shortly.

Mike

Eugene  on January 10th, 2010

Have a little snippet.

#if TARGET_IPHONE_SIMULATOR

#define SLQLOG(…) NSLog(__VA_ARGS__);

#elif TARGET_OS_IPHONE && DEBUG

#define SLQLOG(…) NSLog(__VA_ARGS__);

#else

#define SLQLOG(…);

#endif

Haven’t checked it. If anyone has a better solution, please let me know.

mike  on January 10th, 2010

Thanks Eugene

I’ve made all the necessary changes so that Global.h is not needed and SLQLOG is now NSLog. If you use the link above now you will get the new version.

See how that goes :o)

Mike

Simon  on January 10th, 2010

Mike,

Do you have any plans to produce a tutorial on keeping score on screen. I’d be keen to see how you’re doing it in SLQ.

Also in your one of your videos you said you were going to theme a few UIViews is there something you can share with us on this area? Also I understand that this can be quite resource intensive how are you finding it?

Thanks

Simon

benoitr007  on January 11th, 2010

Hi Mike,

Thank you very much!

What is the expected behavior when locking and unlocking the phone when the music is playing? I’m getting weird results, but they’re always the same: when I lock the phone, the music fades out and stops. When I unlock it, it doesn’t come back; I have to resume it with the resumeMusic method. However, when it’s been paused and resumed one time, the music does come back automatically when I lock & unlock the phone. The next time it doesn’t, I resume it, next time it does, etc. Anyone else is getting similar problems?

Also, a sound file stopped working completely once I upgraded to the new class. I have absolutely no idea why.

Benoit

benoitr007  on January 11th, 2010

Correction to my last message: When I lock & unlock the phone for the first time in my app, I have to PAUSE THE MUSIC and then resume it for it to come back. The resumeMusic method alone doesn’t do anything.

benoitr007  on January 11th, 2010

Perhaps I should ask: are we meant to pause the music manually when the iPhone is locked, and resume it when it’s unlocked?
(Sorry for the triple post)

mike  on January 12th, 2010

Hi benoitr007

There is no reason to pause music before the phone is locked. That should all be handled by the sound manager. I’m using the same code in SLQ where I have some ambient sound playing as the background music as well as sound effects. When the phone is locked, an alarm sounds or the phone rings the sounds are faded out and then back int again when you go back to the game.

Have you tried this on the real device. Sound does not work very well on the Simulator and there have been a bunch of problems with testing sound on the Sim. It works fine on the real device though.

Mike

benoitr007  on January 12th, 2010

Hi Mike,

Thanks for clarifying this. I found out why it didn’t work: it’s because I set the audio session to SoloAmbient using this code at the beginning of the game:

UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

If I don’t do that, I get a very noticeable lag every second or so (as I mentionned before). I’m surprised you don’t have this problem, because when I run the tutorial 10 project with only a music added on the actual device, it does happen.

If someone can help me figure out how to keep the music after locking & unlocking the phone while having the audio session set to SoloAmbient, it would be very appreciated! Thanks.

benoitr007  on January 12th, 2010

Oh, I forgot to mention, I’m currently using a m4a file for the music (compressed with iTunes). I tried mp4 and wav with similar results.

mike  on January 12th, 2010

Hmmm, I’m not using a compressed sound file and hence don’t need hardware decoding which is what SoloAmbient will give you. I’m using CAF and its working fine. I will have a look at this though.

Mike

benoitr007  on January 12th, 2010

Hi Mike,

Thank you so much! CAF is the perfect format. No lag at all in Ambient sound. I used iTunes to convert my WAV music to AIFF, which I renamed to CAF (apparently they’re the same thing). I’m not sure why WAV didn’t work (I noticed it was a bit better than MP3 and M4A, but still chunky). All the sound effects in my game are in WAV format, should I convert them to CAF too?

I’m still getting occasional lags in my game, when there are a lot of images on the screen for instance. You mentioned in your tutorials that you had a new method for batch rendering; I’m not sure how that can be done, do you plan on making a future tutorial on that? I’m definitely getting the book no matter what though!

Thanks again!

JeffHod  on January 13th, 2010

Hi Mike,

Been a long time reader, first time poster. Can i say firstly your website has been invaluable in helping me & my friends grasp concepts in iPhone Development, you should be awarded some sort of Nobel Prize.

I’m at the stage in my game where im putting sounds in. It seems for some reason your old sound effects class wouldnt work for me. I added the new classes you posted on this page, but again no sound effects are playing, it prints an error on the console thrown by the soundmanager(although the background music works). I also removed any troublesome plugins that were mentioned before in posts.

Also noticed this code snippet in the code published here:

- (void)stopSoundWithKey:(NSString*)theSoundKey {
// TODO: complete this method
}

Can you provide some small sample code of the new usage of these sound classes so that i can narrow down why my fx arent playing.

Benoit  on January 13th, 2010

JeffHod, I too had some sounds that wouldn’t play at all. I had to convert them multiple times to other formats, try different lengths and bitrates before it would finally work. Can you upload one of your FX and post the link here, I would check if it works in my game project and if it doesn’t, convert it so it does.

JeffHod  on January 13th, 2010

Hi Benoit, thanks for your reply, yeah i thought that might be it too, but was using the laser.caf and laser.wav files in Daves projects, which worked for him. Maybe try those files to see if they work for you.

Benoit  on January 15th, 2010

JeffHod, I tried these files on my end. Using the new sound manager class on the device, mylaser.wav never plays, but laser.caf does. :S

Also, Mike, my CAF music is HUGE in terms of file size (approximately 15 MB). I’m not sure this is good for my app, if you see what I mean. By chance, do you have any idea how I could make it smaller (maybe decrease the quality a bit, or another format) while keeping it quickly decodable? At the moment I’m stuck between 2 options: make it small and have lag problems or all sorts of sound-related issues, or make it huge.

Thanks!

mike  on January 15th, 2010

Hi guys

I have been away on business this week and just catching up on the comments. I’ll go through them later in detail and get some responses done.

Cheers

Mike

benoitr007  on January 18th, 2010

Hey Mike,

I’ve done some progress. First I have fixed all the problems I had with SoloAmbient sessions by modifying the sound manager class. It creates a SoloAmbient session unless the iPod is playing when the app is launched (in which case it creates an Ambient session and doesn’t play my music). The music resumes just fine after unlocking the phone, and I have absolutely no lag problems. I recommend you this method because it permits the use of compressed MP3 files which are about 500% smaller than uncompressed CAF’s!

Also I discovered that since you updated the sound manager class, Instruments reports “AudioToolBox” leaks on both the device and the simulator… Just wondering!

Thanks,

Benoit

mike  on January 18th, 2010

Hi benoitr007

Your change sounds really good in identifying if the iPod is playing or not and setting the sound category as necessary. I will have to look at adding that to my sound class. I’ve JUST started to use background sound that is MP3 and so change the sound category to SoloAmbient to get the performance I need. It does make a difference using a compressed file for the music which is very useful.

Thanks for the feedback benoitr007. That is going to be really useful for people.

Simon, sorry for missing your post. I will be covering something like score on screen as soon as possbile.

Mike

HK-Paul  on January 19th, 2010

Hi Mike,
I have some updates to the sound manager too – just wondering what’s the best way I could share that – e.g stopSound/stopAllSounds/updateALSourcePosition.

Benoit – were you using CAF files for background music then? Is that because MP3 was being software decoded causing a performance issue? I’ve been able to use MP3 with no issues with the latest sound manager that Mike posted (although I made some changes as I’m working with C++ and needed the stopSounds functionality).
I also experienced some troubles getting some CAFF samples to play – having to re-record and convert some of them.. couldn’t figure out what was the problem.

BTW, anybody, if the openAL on iPhone supports 32 sources, is that 32 stereo sources? or 32 mono sources?
I’ve been using stereo effects sounds and have been able to use the positioning functionality of openAl without issue.

Mike, finally, I would like to plug my new (and first ever) blog – http://www.2d-cubed.com (it’s a bit basic – unfortunately I don’t have a lot of time to make it prettier).
I’m going to be talking about my iPhone game development there, and will be mentioning 71 Squared.com, as I’ve gotten some excellent tips from here – thanks again.
I’m hoping to release a game shortly I’ll also be using my blog to post pictures / videos and info about that in the coming weeks.
I may also be seeking some beta testers for my game, especially anybody who is interested in reciprocal help of that nature from myself and also because I don’t have a 3GS device – so don’t know how it will perform on those.

Thanks,
Paul.

benoitr007  on January 19th, 2010

Hi Paul,

Yes, I had performance issues with mp3′s in Ambient audio. Only uncompressed caf’s made the game run smoothly.

JeffHod  on January 21st, 2010

Hey,

No my sound manager still isnt working.

On init:
INFO – SoundManager: Loaded sound with key ‘laser’ into buffer ’2416′

On call:
ERROR – SoundManager: The music key ‘laser’ could not be found

Can someone provide a complete implementation with some example code? Would be greatly appreciated.

Also its sooooo leaky!! Has anyone else found this?

Jeff

mike  on January 22nd, 2010

Hi Jeff

It looks like you are loading a sound affect but then trying to play a music track. They are two different things in the sound manager. To play a sound affect you need to use playSoundWithKey rather than playMusicWithKey.

If that is not the problem let me know.

I am going to be posting a new version of the sound manager very soon which also has the memory leaks fixed as well. So keep an eye out for that.

Mike

Leave a Comment