Play mp3, ogg, wav...?


I am trying to find on the forums for that response but i haven’t any luck.
Is possible to play any sound / music files for games written in codea? If not, what can be played?

Thanks in advance,

Regards, fjsantos.

Hi @fjsantos, this forum is hard to search, but try searching by the sound or music tags. Sound files are not able to be loaded easily like images, but you can create simple sound effects and music using sound(). What are you after and how much trouble are you willing to go to? :slight_smile:

Hi @Fred, thanks so much for reply!

I am really looking for a simple method to play some music / sounds for my games written in codea; I know about the method ‘sound’, but I think is a bit poor for games :(.

Is there any possibilities to include it after on XCode or something?

Thanks in advance!

Regards, fjsantos.

Hi @fjsantos, you could do it in Xcode if you knew ObjectiveC I guess. I haven’t seen an example of how to do this I’m afraid.

.@Fred @fjsantosb
@juaxix has used this in his project but i never gave it a shot

Thanks @veeeralp, I should try it too.

Hi @Fred, @veeeralp,

It’s a great point to have a library that implements music mp3 files (and GameCenter too).
That open a great world of possibilities over the Codea limitation and it makes possible to create great games.

I will take a look over it.

Thanks so much for your help & support!

Regards, fjsantos.

This is relatively easy to do in Xcode if you just want to loop a soundtrack.

In your header file define your music player like this:

AVAudioPlayer   *player;

Then in the main file:

- (void)initMusicPlayer
    if (player == nil)
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Darkness" ofType:@"mp3"];
        NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
        player = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
        player.numberOfLoops = -1; //play infinite - i.e. keep looping

- (void)viewDidAppear:(BOOL)animated
    // Setup Background Music if Music Option is Yes
    BOOL playMusic = [[NSUserDefaults standardUserDefaults] boolForKey: kGameMusic];
    if (playMusic)
        [self initMusicPlayer];
        [player play];
        [player stop];
        player = nil;

Reefwing, I’m a little slow at this. Could you explain a little more?
Say I manage to run my codea app on Xcode (simulator and iphone), which header file and main file should I add the above code to? Also, how would I implement it on the lua code?

Another issue is where would I store the mp3 file? Is it under main folder with the CodeaTemplate.xcodeproj?