User Tag List

Results 1 to 9 of 9

Thread: iPod music during game - Solved! (I think)

  1. #1
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    iPod music during game - Solved! (I think)

    I recall this question being asked a couple of times in the past - has anyone made any advances in playing iPod music while a game is running?

    I experimented a little this morning with trying to add an iPodMusicPlayer to the CSoundPlayer. Bear in mind throughout this that I know nothing about Objective-C and these few lines took about an hour of research to put together:

    In CSoundPlayer.h:
    Code:
    #import <MediaPlayer/MediaPlayer.h>
    @property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
    In CSoundPlayer.m, do the same import, synthesize the property (whatever this means), and start it up in initWithApp:

    Code:
        self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
        MPMediaItem *currentItem = self.musicPlayer.nowPlayingItem;
        NSString *blah = [currentItem valueForProperty:MPMediaItemPropertyTitle];
        [self.musicPlayer play];
    I then get iPod music for about two seconds, and then it crashes with an EXEC_BAD_ACCESS on:

    Code:
    [pApp->ALPlayer beginInterruption];
    (It's near the top of CSoundPlayer in audioSessionListenerCallback. XCode doesn't have line numbers enabled by default for some reason and I can't find the option to turn them on.)

    Can anyone build on this?

  2. #2
    Clicker Fusion 2.5 DeveloperFusion 2.5 DeveloperAndroid Export ModuleiOS Export ModuleXNA Export Module
    DistantJ's Avatar
    Join Date
    Jan 2008
    Location
    Gloucester, UK
    Posts
    2,142
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not sure what to suggest for the crash but you said you don't know what synthesizing the property means, Obj-C uses objects - sort of like variables but with their own behaviors. Almost like objects in MMF in that they have their own mutable internal values etc. You declare variables, objects, properties etc, in the header (.h) file, and then you refer to it in the implementation (.m) file. Synthesizing sets up getters and setters so that you can interact with the object's properties, values etc.

  3. #3
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thank you for that...! Every other language I've tried I've been able to pick up fairly quickly - the combination of Objective-C and XCode really do make it like learning to code all over again.

    I imagine that it's not easy to do this, or else someone would have worked something out long before me - perhaps I can start finding my way around and finding why this might knock something off in beginInterruption... (or perhaps pApp or ALPlayer aren't initialized for some reason? I don't think I checked...)

  4. #4
    Clicker Fusion 2.5 DeveloperFusion 2.5 DeveloperAndroid Export ModuleiOS Export ModuleXNA Export Module
    DistantJ's Avatar
    Join Date
    Jan 2008
    Location
    Gloucester, UK
    Posts
    2,142
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Think of objects like functions basically. Once you're out of all the object talk, Obj-C is pretty much like any other coding (not script, websites are so much easier because you don't have to declare variables, manage memory etc.) language.

    I *think* EXEC_BAD_ACCESS usually arises from memory management issues. You often have to allocate memory to something and then release that memory segment ([[MPMusicPlayerController alloc] init]; and [release MPMusicPlayerController], check if this is somewhere in there... Memory management drives me freaking nuts, it's the main reason I stick with MMF instead of Cocos2D.

    It would appear that beginInterruption is a command for the object ALPlayer, try looking inside of ALPlayer in whatever file it's stored in for beginInterruption, see if you can see what it does (or paste it here). I'm still something of a beginner with Obj-C myself. I've made a handful of apps entirely in Obj-C but it still feels a little alien to somebody used to linear scripting.

    I also noticed this: NSString *blah = [currentItem valueForProperty:MPMediaItemPropertyTitle];

    Have you declared NSString *blah? The * indicates it's a pointer to a memory slot. What is this line for?

  5. #5
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Memory management is something I'm... not good at either. You're the computer! Work it out for yourself.

    Still, to my surprise I've actually made some further progress - thanks for your advice. I'm not sure what exactly did it, but attaching the music player to CRunApp instead of CSoundPlayer as above and putting the following in load makes iPod music start up on entering the application:

    Code:
        [[MPMusicPlayerController alloc] init];
        self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
        [self.musicPlayer play];
    The NSString and the currentItem in my last post were just variables set up to see if I could retrieve the iPod music (which I could) - to use them safely, would I have had to declare them in a way beyond... declaring them like that? (Another thing to allocate memory for?)

    Anyway, this approach is a tiny step forward, but the application loses all its other sound and will mute the music on returning from sleep (just somewhere else where I need to find the method or function or whatever Objective-C calls this).

    If anyone else who knows what they're doing can offer further enlightenment...!

  6. #6
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Er, I think I've done it. I was completely overcomplicating it by adding an alternative media player - I read up on audio sessions, and all you have to do is change the session category of the existing one. On lines 47 and 48 of CSoundPlayer.m, change this:

    Code:
    UInt32 sessionCategory=kAudioSessionCategory_SoloAmbientSound;
    AudioSessionSetProperty(kAudioSessionCategory_SoloAmbientSound, sizeof(sessionCategory), &sessionCategory);
    To this:

    Code:
    UInt32 sessionCategory=kAudioSessionCategory_AmbientSound;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
    (Note the change of the first argument to AudioSessionSetProperty... is the property name in the existing code a mistake?)

  7. #7
    Clicker Multimedia Fusion 2 DeveloperiOS Export ModuleSWF Export ModuleXNA Export Module

    Join Date
    Jul 2010
    Location
    Italy
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! And do sound effects work with this approach? Also, is it automatically managed the case that if there is an mp3 in the game iPod music stops, and if no mp3's are playing you can listen to iPod music again?
    I'll definitely try it and let you know if it works for me. Thank you for working on this!

  8. #8
    Clicker Multimedia Fusion 2 DeveloperAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleSWF Export ModuleXNA Export ModuleMac Export Module
    colej_uk's Avatar
    Join Date
    Nov 2006
    Location
    UK
    Posts
    739
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Cool Allowing someone to have their ipod running instead of in-game music is the last big 'feature' that I would like that is missing from the exporter.

    I would rather have the game not attempt to play any of it's mp3s/music if the ipod is already running, but I don't know the specifics of how the audio code works.

  9. #9
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recall an option in the sound dialog in MMF when the exporter is selected, a checkbox to play the sound using the media player (I can't remember the exact wording) - it's possible that this option would duck the iPod music when that sample is playing, but I haven't tried it out.

    My game, fortunately, contains no music by default - so all the above does is allow iPod music to play along with the sound effects.

Similar Threads

  1. Allowing iPod music in a game
    By DavidN in forum iOS Export Module Version 2.0
    Replies: 1
    Last Post: 31st May 2015, 04:24 AM
  2. iPod Music during game?
    By netninja in forum iOS Export Module Version 2.0
    Replies: 1
    Last Post: 27th April 2015, 02:54 PM
  3. [REQUEST] iPod Music during app
    By UrbanMonk in forum iOS Export Module Version 2.0
    Replies: 5
    Last Post: 3rd March 2014, 09:16 PM
  4. Fortitude! New game for iPhone, iPod Touch and iPad.
    By Pineapple in forum iOS Released Games & Apps
    Replies: 10
    Last Post: 2nd January 2013, 12:08 AM
  5. My game was rejected because game music plays over phone music
    By leuvsion in forum XNA Export Module Version 2.0
    Replies: 18
    Last Post: 1st August 2012, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •