Auto mute appliaction if iPod is playing music
by Prashant K Singh · in iTorque 2D · 08/29/2009 (9:57 am) · 8 replies
I am trying to give a functionality for the user to be able to listen to the iPhone music while playing my game. To facilitate this, I want to mute the In-Game sounds and music if the user's iPod application is running and my game is launched. Is there a way to detect programmatically if the iPod application is running.
I already know how to stop the iPod from pausing when my application is launched. All I need to know is, if the iPod is playing something, so that I can mute my application.
I already know how to stop the iPod from pausing when my application is launched. All I need to know is, if the iPod is playing something, so that I can mute my application.
#2
08/29/2009 (3:57 pm)
It would be nice to have a resource that you can add to our current iTGB instead of waiting to 1.3.
#3
developer.apple.com/iphone/library/documentation/Audio/Conceptual/iPodLibraryAcc...
Two methods need to be made and registered for notifications if you want to respond to starting and stopping music in the game. The MPMusicPlayerController has a playbackState property which tells you if it's playing or not (and other states).
You need to:
1.Add the MediaPlayer.framework
2.Register at least an MPMusicPlayerControllerPlaybackStateDidChangeNotification
3.Get the shared instance of MPMusicPlayerController
4.Check the current state of this controller when setting up audio
5.If you want to add some fancy support for the media library, implement a MPMediaPickerControllerDelegate with mediaPicker methods, and using the returned MPMediaItemCollection with the MPMusicPlayerController (setQueueWithItemCollection)
I'm listing this so either I or somebody else can implement it in iTorque when time allows ;)
08/29/2009 (6:04 pm)
Start here :)developer.apple.com/iphone/library/documentation/Audio/Conceptual/iPodLibraryAcc...
Two methods need to be made and registered for notifications if you want to respond to starting and stopping music in the game. The MPMusicPlayerController has a playbackState property which tells you if it's playing or not (and other states).
You need to:
1.Add the MediaPlayer.framework
2.Register at least an MPMusicPlayerControllerPlaybackStateDidChangeNotification
3.Get the shared instance of MPMusicPlayerController
4.Check the current state of this controller when setting up audio
5.If you want to add some fancy support for the media library, implement a MPMediaPickerControllerDelegate with mediaPicker methods, and using the returned MPMediaItemCollection with the MPMusicPlayerController (setQueueWithItemCollection)
I'm listing this so either I or somebody else can implement it in iTorque when time allows ;)
#4
You've managed to scare me off :)
My Objective C programming is limited to copy and paste.
But there are lots of very smart people on this forum...
08/29/2009 (6:23 pm)
That's great info.You've managed to scare me off :)
My Objective C programming is limited to copy and paste.
But there are lots of very smart people on this forum...
#5
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:136: error: expected unqualified-id before '@' token
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:138: error: expected constructor, destructor, or type conversion before '*' token
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:139: error: 'NSString' was not declared in this scope
I am not sure why the errors start cropping up in the Foundation Framework. Also, I am compiling for iPhone OS 2.2.1, as iTGB doesn't compile with 3.0 SDK. I tried compiling with 3.0 but I get the same errors for 3.0 too, but the error count is reduced to 998. Any ideas on how to fix this would be a great help.
08/31/2009 (12:26 am)
Ronny, I had already tried the MediaPlayer.framework, but after I include the MediaPlayer/MediaPlayer.h header to my audio.cc file (where audio is getting initialized), I start getting 1990 errors of the likes of below:/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:136: error: expected unqualified-id before '@' token
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:138: error: expected constructor, destructor, or type conversion before '*' token
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:139: error: 'NSString' was not declared in this scope
I am not sure why the errors start cropping up in the Foundation Framework. Also, I am compiling for iPhone OS 2.2.1, as iTGB doesn't compile with 3.0 SDK. I tried compiling with 3.0 but I get the same errors for 3.0 too, but the error count is reduced to 998. Any ideas on how to fix this would be a great help.
#6
Your not going to be able to include it directly in audio.cc. If you want it in audio.cc you need to rename the file to .mm. However I wouldn't do that, I'd add the method's you want to one of the mm files in your iPhonePlatform directory and then expose them C function calls.
08/31/2009 (10:52 am)
Because the file must be named .mm to contain objective c code and c++. Also one you do that any file that includes your header file must also be named .mmYour not going to be able to include it directly in audio.cc. If you want it in audio.cc you need to rename the file to .mm. However I wouldn't do that, I'd add the method's you want to one of the mm files in your iPhonePlatform directory and then expose them C function calls.
#7
(2.0 can be your deployment target for now - everything shipped with the engine should work the same)
If you start with a known good target that compiles against 2.x, change the compiler to GCC 4.2 and SDK to 3.0. Then start fixing the errors. One fix in several places is that you need to remove the class name from methods (they'll be mistakenly set to Class::Method() in the class definition - 4.2 is strict about that). The other fix is in the sound engine used, which you should be able to find instructions for on the net. It's not GG-exclusive.
I recommend making some new .mm files where you test for the existence of the necessary frameworks. Call them then from the audio setup. If running a 3.0+ device, iPod access will be available and some control can be had. If on 2.x, you should probably check if there's some session control you can use. I expect you to only be able to get the opposite result: Pause iPod audio when your game starts.
08/31/2009 (11:39 am)
Apple recommend compiling against the 3.0 SDK, because it is a superset of all the earlier SDKs. Since MediaPlayer is a 3.0+ framework, you also need to weak link it and check if the classes exist before trying to use it, then use it only if found.(2.0 can be your deployment target for now - everything shipped with the engine should work the same)
If you start with a known good target that compiles against 2.x, change the compiler to GCC 4.2 and SDK to 3.0. Then start fixing the errors. One fix in several places is that you need to remove the class name from methods (they'll be mistakenly set to Class::Method() in the class definition - 4.2 is strict about that). The other fix is in the sound engine used, which you should be able to find instructions for on the net. It's not GG-exclusive.
I recommend making some new .mm files where you test for the existence of the necessary frameworks. Call them then from the audio setup. If running a 3.0+ device, iPod access will be available and some control can be had. If on 2.x, you should probably check if there's some session control you can use. I expect you to only be able to get the opposite result: Pause iPod audio when your game starts.
Torque 3D Owner Ronny Bangsund
Torque Cheerleaders