123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //
- // iOSUserMusicLibrary.mm
- // iTorque2D
- //
- // Created by Sven Bergstrom on 2010/05/10.
- // Copyright 2010 Luma Arcade. All rights reserved.
- //
- #ifdef TORQUE_ALLOW_MUSICPLAYER
- #import "iOSUserMusicLibrary.h"
- #import "MediaPlayer/MPMusicPlayerController.h"
- #include "console/console.h"
- #include "console/consoleTypes.h"
- // Global master volume for stock music player
- static F32 gMusicVolume = 0.5;
- @interface iOSUserMusicLibrary : NSObject {
-
- MPMusicPlayerController* musicPlayer;
-
- }
- @property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
- @end
- @implementation iOSUserMusicLibrary
- @synthesize musicPlayer;
- @end
- static iOSUserMusicLibrary* iT2dMusicManager;
- static bool iOSMusicManagerIsActive = false;
- void createMusicPlayer()
- {
- // Specify a media query; this one matches the entire iPod library because it
- // does not contain a media property predicate
- MPMediaQuery *everything = [[MPMediaQuery alloc] init];
-
- // Configure the media query to group its media items; here, grouped by artist
- [everything setGroupingType: MPMediaGroupingArtist];
-
- iT2dMusicManager = [[[iOSUserMusicLibrary alloc] init] retain];
- iT2dMusicManager.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
-
- [iT2dMusicManager.musicPlayer setQueueWithQuery: everything];
-
- Con::printf("iOSCreateMusicPlayer success, music player is enabled.");
-
- iOSMusicManagerIsActive = true;
- iT2dMusicManager.musicPlayer.volume = gMusicVolume;
-
- Con::addVariable("iPodMusicVolume", TypeF32, &gMusicVolume);
- Con::setVariable("$iOSMusicTrack", "Now Playing: ");
- }
- void destroyMusicPlayer()
- {
- iT2dMusicManager.musicPlayer = nil;
- [iT2dMusicManager release];
- iT2dMusicManager = nil;
-
- iOSMusicManagerIsActive = false;
- }
- void updateVolume()
- {
- if(iOSMusicManagerIsActive)
- {
- // Set the iT2DMusicManager.musicPlayer volume property
- iT2dMusicManager.musicPlayer.volume = gMusicVolume;
- }
- }
- ConsoleFunction(iOSCreateMusicPlayer, void, 1, 1, "")
- {
- if(!iOSMusicManagerIsActive)
- {
- createMusicPlayer();
- }
- else
- {
- Con::printf("iOSCreateMusicPlayer was already called.");
- }
- }
- ConsoleFunction(iOSMusicPlay, void, 1, 1, "iOSMusicPlay()")
- {
- if(iOSMusicManagerIsActive)
- {
- [iT2dMusicManager.musicPlayer play];
-
- MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
-
- // Display the artist and song name for the now-playing media item
- NSString* output = [
- NSString stringWithFormat: @"%@ %@ %@ %@",
- NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
- [currentItem valueForProperty: MPMediaItemPropertyTitle],
- NSLocalizedString (@"by", @"Article between song name and artist name"),
- [currentItem valueForProperty: MPMediaItemPropertyArtist]];
- //Hand it off to the compass variable in torque.
- const char* tOutput = [output UTF8String];
- Con::printf(tOutput);
- Con::setVariable("$iOSMusicTrack", tOutput );
- }
- else
- {
- Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
- }
- }
- ConsoleFunction(iOSMusicPause, void, 1, 1, "iOSMusicPause()")
- {
- if(iOSMusicManagerIsActive)
- {
- [iT2dMusicManager.musicPlayer pause];
- }
- else
- {
- Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
- }
-
- }
- ConsoleFunction(iOSMusicStop, void, 1, 1, "iOSMusicStop()")
- {
- if(iOSMusicManagerIsActive)
- {
- [iT2dMusicManager.musicPlayer stop];
- }
- else
- {
- Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
- }
-
- }
- ConsoleFunction(iOSMusicNext, void, 1, 1, "iOSMusicNext()")
- {
- if(iOSMusicManagerIsActive)
- {
- [iT2dMusicManager.musicPlayer skipToNextItem];
- MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
-
- // Display the artist and song name for the now-playing media item
- NSString* output = [
- NSString stringWithFormat: @"%@ %@ %@ %@",
- NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
- [currentItem valueForProperty: MPMediaItemPropertyTitle],
- NSLocalizedString (@"by", @"Article between song name and artist name"),
- [currentItem valueForProperty: MPMediaItemPropertyArtist]];
- //Hand it off to the compass variable in torque.
- const char* tOutput = [output UTF8String];
- Con::printf(tOutput);
- Con::setVariable("$iOSMusicTrack", tOutput );
- }
- else
- {
- Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
- }
-
- }
- ConsoleFunction(iOSMusicPrevious, void, 1, 1, "iOSMusicPrevious()")
- {
- if(iOSMusicManagerIsActive)
- {
- [iT2dMusicManager.musicPlayer skipToPreviousItem];
- MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
-
- // Display the artist and song name for the now-playing media item
- NSString* output = [
- NSString stringWithFormat: @"%@ %@ %@ %@",
- NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
- [currentItem valueForProperty: MPMediaItemPropertyTitle],
- NSLocalizedString (@"by", @"Article between song name and artist name"),
- [currentItem valueForProperty: MPMediaItemPropertyArtist]];
- //Hand it off to the compass variable in torque.
- const char* tOutput = [output UTF8String];
- Con::printf(tOutput);
- Con::setVariable("$iOSMusicTrack", tOutput );
- }
- else
- {
- Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
- }
-
- }
- #endif //TORQUE_ALLOW_MUSICPLAYER
|