iOSUserMusicLibrary.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //
  23. // iOSUserMusicLibrary.mm
  24. // iTorque2D
  25. //
  26. // Created by Sven Bergstrom on 2010/05/10.
  27. // Copyright 2010 Luma Arcade. All rights reserved.
  28. //
  29. #ifdef TORQUE_ALLOW_MUSICPLAYER
  30. #import "iOSUserMusicLibrary.h"
  31. #import "MediaPlayer/MPMusicPlayerController.h"
  32. #include "console/console.h"
  33. #include "console/consoleTypes.h"
  34. // Global master volume for stock music player
  35. static F32 gMusicVolume = 0.5;
  36. @interface iOSUserMusicLibrary : NSObject {
  37. MPMusicPlayerController* musicPlayer;
  38. }
  39. @property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
  40. @end
  41. @implementation iOSUserMusicLibrary
  42. @synthesize musicPlayer;
  43. @end
  44. static iOSUserMusicLibrary* iT2dMusicManager;
  45. static bool iOSMusicManagerIsActive = false;
  46. void createMusicPlayer()
  47. {
  48. // Specify a media query; this one matches the entire iPod library because it
  49. // does not contain a media property predicate
  50. MPMediaQuery *everything = [[MPMediaQuery alloc] init];
  51. // Configure the media query to group its media items; here, grouped by artist
  52. [everything setGroupingType: MPMediaGroupingArtist];
  53. iT2dMusicManager = [[[iOSUserMusicLibrary alloc] init] retain];
  54. iT2dMusicManager.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
  55. [iT2dMusicManager.musicPlayer setQueueWithQuery: everything];
  56. Con::printf("iOSCreateMusicPlayer success, music player is enabled.");
  57. iOSMusicManagerIsActive = true;
  58. iT2dMusicManager.musicPlayer.volume = gMusicVolume;
  59. Con::addVariable("iPodMusicVolume", TypeF32, &gMusicVolume);
  60. Con::setVariable("$iOSMusicTrack", "Now Playing: ");
  61. }
  62. void destroyMusicPlayer()
  63. {
  64. iT2dMusicManager.musicPlayer = nil;
  65. [iT2dMusicManager release];
  66. iT2dMusicManager = nil;
  67. iOSMusicManagerIsActive = false;
  68. }
  69. void updateVolume()
  70. {
  71. if(iOSMusicManagerIsActive)
  72. {
  73. // Set the iT2DMusicManager.musicPlayer volume property
  74. iT2dMusicManager.musicPlayer.volume = gMusicVolume;
  75. }
  76. }
  77. ConsoleFunction(iOSCreateMusicPlayer, void, 1, 1, "")
  78. {
  79. if(!iOSMusicManagerIsActive)
  80. {
  81. createMusicPlayer();
  82. }
  83. else
  84. {
  85. Con::printf("iOSCreateMusicPlayer was already called.");
  86. }
  87. }
  88. ConsoleFunction(iOSMusicPlay, void, 1, 1, "iOSMusicPlay()")
  89. {
  90. if(iOSMusicManagerIsActive)
  91. {
  92. [iT2dMusicManager.musicPlayer play];
  93. MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
  94. // Display the artist and song name for the now-playing media item
  95. NSString* output = [
  96. NSString stringWithFormat: @"%@ %@ %@ %@",
  97. NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
  98. [currentItem valueForProperty: MPMediaItemPropertyTitle],
  99. NSLocalizedString (@"by", @"Article between song name and artist name"),
  100. [currentItem valueForProperty: MPMediaItemPropertyArtist]];
  101. //Hand it off to the compass variable in torque.
  102. const char* tOutput = [output UTF8String];
  103. Con::printf(tOutput);
  104. Con::setVariable("$iOSMusicTrack", tOutput );
  105. }
  106. else
  107. {
  108. Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
  109. }
  110. }
  111. ConsoleFunction(iOSMusicPause, void, 1, 1, "iOSMusicPause()")
  112. {
  113. if(iOSMusicManagerIsActive)
  114. {
  115. [iT2dMusicManager.musicPlayer pause];
  116. }
  117. else
  118. {
  119. Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
  120. }
  121. }
  122. ConsoleFunction(iOSMusicStop, void, 1, 1, "iOSMusicStop()")
  123. {
  124. if(iOSMusicManagerIsActive)
  125. {
  126. [iT2dMusicManager.musicPlayer stop];
  127. }
  128. else
  129. {
  130. Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
  131. }
  132. }
  133. ConsoleFunction(iOSMusicNext, void, 1, 1, "iOSMusicNext()")
  134. {
  135. if(iOSMusicManagerIsActive)
  136. {
  137. [iT2dMusicManager.musicPlayer skipToNextItem];
  138. MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
  139. // Display the artist and song name for the now-playing media item
  140. NSString* output = [
  141. NSString stringWithFormat: @"%@ %@ %@ %@",
  142. NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
  143. [currentItem valueForProperty: MPMediaItemPropertyTitle],
  144. NSLocalizedString (@"by", @"Article between song name and artist name"),
  145. [currentItem valueForProperty: MPMediaItemPropertyArtist]];
  146. //Hand it off to the compass variable in torque.
  147. const char* tOutput = [output UTF8String];
  148. Con::printf(tOutput);
  149. Con::setVariable("$iOSMusicTrack", tOutput );
  150. }
  151. else
  152. {
  153. Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
  154. }
  155. }
  156. ConsoleFunction(iOSMusicPrevious, void, 1, 1, "iOSMusicPrevious()")
  157. {
  158. if(iOSMusicManagerIsActive)
  159. {
  160. [iT2dMusicManager.musicPlayer skipToPreviousItem];
  161. MPMediaItem *currentItem = [iT2dMusicManager.musicPlayer nowPlayingItem];
  162. // Display the artist and song name for the now-playing media item
  163. NSString* output = [
  164. NSString stringWithFormat: @"%@ %@ %@ %@",
  165. NSLocalizedString (@"Now Playing:", @"Label for introducing the now-playing song title and artist"),
  166. [currentItem valueForProperty: MPMediaItemPropertyTitle],
  167. NSLocalizedString (@"by", @"Article between song name and artist name"),
  168. [currentItem valueForProperty: MPMediaItemPropertyArtist]];
  169. //Hand it off to the compass variable in torque.
  170. const char* tOutput = [output UTF8String];
  171. Con::printf(tOutput);
  172. Con::setVariable("$iOSMusicTrack", tOutput );
  173. }
  174. else
  175. {
  176. Con::printf("iOSMusicPlayer is not active, did you call iOSCreateMusicPlayer(); ? ");
  177. }
  178. }
  179. #endif //TORQUE_ALLOW_MUSICPLAYER