Audio support for my Lemmings port

4 views
Skip to first unread message

a...@bethere.co.uk

unread,
Mar 13, 2009, 9:07:33 PM3/13/09
to Big Five Safari iPhone Apps
Hi

I am working on a port of the classic game Lemmings for the iPhone and
so far it works great, however one thing is missing and that's audio
support.

I expect it to be relatively straight forward to add, maybe some of
you have already added it to Big5's source code, I would not have a
clue as I'm not an Objective C programmer, but I am a very advanced
Javascript / web dev guy.

I would be really grateful if anyone could help me out, and advise me
on how to get javascript sound working in Big5 like it is in Phonegap.

Phonegap currently has a performance overhead which makes my game
twice as slow compared to Big5. So if I cannot get audio on Big5 I
will have to choose between slower performance or no-sound effects &
music :-(

Cheers - Andrew
Nanopunk Studios Manchester
www.npunk.com

awaybbl

unread,
Mar 16, 2009, 6:55:25 PM3/16/09
to Big Five Safari iPhone Apps
not sure if this will help you...

in big5ViewController.m I added (after the vibration routine):

// soundPlay
else if ([command isEqualToString:@"soundPlay"]){

NSLog(@"soundPlay: request!");

CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;

// Get the main bundle for the app
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (
mainBundle,
CFSTR ("pop2"),
CFSTR ("aif"),
NULL
);

// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef,
&soundFileObject);
// Play it
AudioServicesPlaySystemSound (soundFileObject);
}

/////////

Then in the big5.js I added (again after the vibration function):

soundPlay: function() {
// setting_vibration: true;
return Device.exec("soundPlay")
},


///

then in my app I just call Device.soundPlay();

The only thing to remember is that this routine uses the "system
sound" routines, and are therefore limited to about 20 seconds of
sound, and I've had best success with *.aif file format.

a...@bethere.co.uk

unread,
Mar 16, 2009, 7:15:58 PM3/16/09
to Big Five Safari iPhone Apps
Thank you very much, that looks like it will work great for sound FX.

I'm awaiting what will happen with the 3.0 OS software, revealed to
the press tomorrow. Wouldn't it be nice to have HTML 5 audio support,
i.e. <audio src="soundtrack.mp3">

(And as a web developer not used to Cocoa & Objective C++, wonder why
life isn't so simple when writing native code!!!)

awaybbl

unread,
Mar 16, 2009, 8:30:13 PM3/16/09
to Big Five Safari iPhone Apps
I wouldn't count on <audio src="soundtrack.mp3"> support since Apple
probably wouldn't want some to build their own music player

a...@bethere.co.uk

unread,
Mar 17, 2009, 7:07:00 PM3/17/09
to Big Five Safari iPhone Apps
Hey awaybbl

Please excuse my ignorance of Cocoa Touch but is the path to the AIF
sound file specified in the Javascript function, i.e. Device.playSound
('test.aif') or in the C++ of big5viewcontroller.m?

I've been unable to get the sound to play, and I guess I'm not using
the right path or formatting it in the right way - maybe I need to use
an absolute path rather than a relative one, and if so is that path
going to be a HTTP one or a internal iPhone file system one like /var/
apps/... etc.?

The good thing is that your code seems to be compiling and linking
into my JS fine, as putting a call in the C++ playSound routine to
vibrate the phone works when the Device.playSound function is called
from within my JS.

Cheers - Andrew

a...@bethere.co.uk

unread,
Mar 17, 2009, 7:42:40 PM3/17/09
to Big Five Safari iPhone Apps
Ahh no - got it!!

The file has to be in the Resources directory of the XCode Project to
be accessed from the Javascript web app.

It works like a treat - I can't thank you enough, thanks. Credit due
in game screen.

PS - does anyone know if 20+ secs backing music is possible with Big5?

PPS - this has sealed it - bye bye slow Phonegap.

a...@bethere.co.uk

unread,
Mar 18, 2009, 11:27:38 AM3/18/09
to Big Five Safari iPhone Apps
After initial excitement there, I found a snag. The URL to the AIFF is
hard coded into the Objective C side - can a parameter be passed from
the Javascript function into the C routine, by ways of specifying the
sound FX filename in my Javascript code?

I.e. Device.soundPlay('mysoundfile.aif')

Thanks once again - Andrew

awaybbl

unread,
Mar 18, 2009, 7:37:08 PM3/18/09
to Big Five Safari iPhone Apps
Andrew,

Of course...

My example was a bare bones example.

another way to make it more generic would be to pass a parameter... so
let's make a new routine called Device.soundPlayAIF("filename")

now I've intentionally left the aif off the filename. this routine
should always use .aif files (highly recommended for performance).

OK...

IN big5.js....
////
soundPlayAIF: function(name) {
// setting_vibration: true;
return Device.exec("soundPlayAIF",name)
},

IN big5ViewController.m

else if ([command isEqualToString:@"soundPlayAIF"]){

NSLog(@"soundPlayAIF: request!");

CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;

// Get the main bundle for the app
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (
mainBundle,
param,
CFSTR ("aif"),
NULL
);

// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef,
&soundFileObject);
// Play it
AudioServicesPlaySystemSound (soundFileObject);
}
.......

then in your code...

Device.soundPlayAIF("pop2");


....

I did get a compiler warning but it does work... enjoy

a...@bethere.co.uk

unread,
Mar 18, 2009, 8:32:51 PM3/18/09
to Big Five Safari iPhone Apps
Hey Enrico!

That works brilliantly. Thank you once again.

I am really pleased to now start work on the sound FX, and I am
working on using the AVFoundation framework to bring full length music
tracks into Big5. I am learning about Objective C as I go, so any more
help on audio would be very much appreciated!

PS - if you have any projects of your own Enrico, and would like to
mention them in the credits page of my game, let me know.

Cheers! Andrew

Dirk Holtwick

unread,
May 4, 2009, 3:35:52 AM5/4/09
to big5...@googlegroups.com
Hi,

thanks for the sound code. I tried to put it into the app, but for the
AppStore version I would have to add a download function etc. Since the
iPhone OS 3.0 will make big progress in this area I think this issue
will become deprecated then.

Dirk

a...@bethere.co.uk schrieb:
Reply all
Reply to author
Forward
0 new messages