Another New(ish) Tutorial (28) - Codea v1.5.2: Objective C Add On, Audio

The latest version of Xcode also makes it fairly easy to extend Codea with Objective C functionality (e.g. Game Center, iAds, and in-App purchases). One of the most repeated requests is the ability to play audio. The method previously described in our tutes is no longer the best or easiest way to achieve this, so here is an updated tute showing how to get add ons working with your exported Codea project. The first example is an audio add on which allows you to play and stop an mp3 (for example - other codecs are supported) and display the power levels going to the left and right speakers (i.e. a dB meter):

We used the most excellent Cider controls from @Mark to demo the add on.

@tnlogy has done another example of a face detection add on (and save to photo album and facebook share) -

Let me know if there is anything else that you would like to see. An iAds add on should be posted this weekend.

I have a request for an x-code/Codea tutorial which I think many will be interested in.
How to listen for Udp packets and write to them.
I am thinking that a simple string could be the link in to Codea. In the end I think it could be very simple to achieve but not knowing the first thing about x-code makes it a bit hard to overview.

Re Peter

@macflyerdk - the UDP stuff is fairly low level and I have to admit that I’m no network code guru. I’m not sure I even know enough to test an add on like this. I could probably do an add on which hooked into an existing library (e.g. - would that work?

Yes, using a predefined library would be a good idea.
I picture it like two boxes, one is Codea code and the other Xcode library. Then they must be able to comunicate continuesly using just a string…a least that is my imagination.

Yesterday I signed up for the Apple developer program, got my Codea code imported to Xcode and made it run on an iPad :slight_smile: Launch day for my first app is closing in now.
I have a server solution including Mamp server and php scripts ready so it will not be dependent on udp inside the code, but sure would be nice to not ask the user for a setup that could fail and leave the user very diapointed in the end.

Appreciate you work on the tutorials. They are realy helpfull :slight_smile:

@Reefwing - Cool tutorial, but I still think that would be awesome to add audio file On The Fly in Codea, because I want to hear background music when playing game in development. But, Things like this will not happen - Maybe 2014 will be possible. :-B

@Georgian - Thanks dude.

@Simeon is working on native audio for Codea. I’m amazed at the amount of code that the folks at TLL are able to pump out in their spare time. Remember that none of us are paying an extra cent for all this great additional functionality. There aren’t many apps in iTunes that get this level of support.

@macflyerdk - I’ll have a look at it and maybe you can improve it if I get something basic working. Like I said, network coding is not my bag! Good luck with your first app - that is very exciting!

@Reefwing In your game center integrating tutorial, I can’t find LuaState.m in my project. Are you going to make a game center add on tutorial?

Hi @wave - that method of getting Game Center working was for the old runtime. @Simeon has put together a quick add on for Game Center which works with the new Xcode export here:

Let me know if you get stuck and I will do a tute on how to use it.

@Reefwing Thank you, I got an error from DEFINE_SHARED_INSTANCE_IMPL and not quite sure how to use it in my project. I’m not familiar with xcode. Can you give me some instructions?

@wave - no worries, I’ll do a quick tute tomorrow.

@wave - Game Center tute is up:

Let me know how you go. Note that it takes a while (hours?) for iTunes Connect to recognise that your app is enabled for Game Center so dont be too worried if the player can’t authenticate initially.

@Reefwing For what I see this add on doesn’t have a saveScore (report score) function right? I’m trying to add it from your previous tutorial (tutorial 21). Can I just declare it in the header like other function and copy the implementation part in?

@wave - yes just follow the pattern if you want to add more functions. In GameCenterAddOn.h add:

static int saveScore(struct lua_State *state);

Then in GameCenterAddOn.m add/modify:

// Add to method

- (void)codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
    lua_register(L, "saveScore", saveScore);

// New Objective C method

- (void) saveNewScore: (int) score
    // Replace "Easy Difficulty" with your identifier from iTunes Connect

    GKScore *scoreReporter = [[GKScore alloc] initWithCategory: @"Easy Difficulty"];

    if (scoreReporter)
        scoreReporter.value = score;
        [scoreReporter reportScoreWithCompletionHandler: ^(NSError *error)
            if (error != nil)
                // handle the reporting error

                NSLog(@"Game Center: Error Saving Score - %@", [error localizedDescription]);

// New C function

static int saveScore(struct lua_State *state)
    [gameCenterAddOnInstance saveNewScore: lua_tonumber(state, 1)];
    return 0;

Then in your Lua code use saveScore(yourNewScore)

@Reefwing Thank you. I wouldn’t be able to get it working in my app without your help.

@wave - happy to help. Good luck with the app, I look forward to downloading it from iTunes.