iOSMoviePlayback.mm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifdef TORQUE_ALLOW_MOVIEPLAYER
  23. //iOS media stuff
  24. #include "iOSMoviePlayback.h"
  25. #import "MediaPlayer/MPMoviePlayerController.h"
  26. #import "MediaPlayer/MPMoviePlayerViewController.h"
  27. #import "AudioToolbox/AudioServices.h"
  28. //#include "audio/audio.h"
  29. //#include "core/strings/stringFunctions.h"
  30. //Torque Stuff
  31. #include "console/console.h"
  32. #include "platformiOS.h"
  33. extern iOSPlatState platState;
  34. ///ObjC section below
  35. //Declaring the interface here, so that i can use the header for inclusion in C++ code
  36. @interface iOSMoviePlaybackDelegate : NSObject {
  37. MPMoviePlayerViewController* movieViewController;
  38. }
  39. @property (nonatomic, retain) MPMoviePlayerController* movieController;
  40. -(NSURL *) urlForLocalFile:(NSString*)fileLocation withFileType:(NSString*)fileType;
  41. -(BOOL) playAVideo:(NSString*)fileName withFileType:(NSString*)fileType withScalingMode:(MPMovieScalingMode)scalingMode withControlStyle:(MPMovieControlStyle)controlStyle;
  42. -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
  43. @end
  44. @implementation iOSMoviePlaybackDelegate
  45. @synthesize movieController;
  46. //Wanted to keep the ObjC stuff neat and not littered with C++, implemented the C++ inside the playVideo function instead (the one exposed to the engine).
  47. -(NSURL *)urlForLocalFile:(NSString*)fileLocation withFileType:(NSString*)typeName
  48. {
  49. NSURL* movieURL = nil;
  50. //Find the file location within our application bundle
  51. NSBundle *bundle = [NSBundle mainBundle];
  52. if (bundle)
  53. {
  54. //Actually, we need to use inDirectory or else it only looks in the toplevel directories.
  55. //Cut the file name from the end of the string
  56. NSString* fnameOnly = [fileLocation lastPathComponent];
  57. NSString* pathOnly = [fileLocation stringByDeletingLastPathComponent];
  58. //Ask the bundle for the path to the file
  59. NSString *moviePath = [bundle pathForResource:fnameOnly ofType:typeName inDirectory:pathOnly];
  60. if (moviePath)
  61. {
  62. movieURL = [NSURL fileURLWithPath:moviePath];
  63. }
  64. }
  65. //Check if its nil when you use it, if there was some kind of error
  66. return movieURL;
  67. }
  68. -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  69. {
  70. return YES;
  71. }
  72. //This is the last step of the video call, the C++ hands in the values we want here (scaling etc). NO EXTENSION on the filename.
  73. -(BOOL) playAVideo:(NSString*)fileName withFileType:fileType withScalingMode:(MPMovieScalingMode)scalingMode withControlStyle:(MPMovieControlStyle)controlStyle
  74. {
  75. NSString* defaultFileType = fileType;
  76. NSURL* filePath = [self urlForLocalFile:fileName withFileType:defaultFileType];
  77. if(filePath == nil)
  78. {
  79. return NO;
  80. }
  81. movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL: filePath];
  82. [[NSNotificationCenter defaultCenter]
  83. addObserver: self
  84. selector: @selector(moviePlaybackDidFinish:)
  85. name: MPMoviePlayerPlaybackDidFinishNotification
  86. object: nil];
  87. //Disable audio session
  88. AudioSessionSetActive(NO);
  89. //Audio::OpenALShutdown();
  90. [movieController setScalingMode:MPMovieScalingModeAspectFit];
  91. [movieController setControlStyle:MPMovieControlStyleNone];
  92. [platState.Window addSubview:movieViewController.view];
  93. //[movieViewController setControlStyle:MPMovieControlStyleNone];
  94. [movieController prepareToPlay];
  95. [movieController play];
  96. //[[movieViewController moviePlayer] play];
  97. return YES;
  98. }
  99. //Notification of when the video has done playing now, and we can move on.
  100. - (void) moviePlaybackDidFinish:(NSNotification*)notification
  101. {
  102. //Remove our observer
  103. [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
  104. [movieViewController.view removeFromSuperview];
  105. // Reactivate the current audio session
  106. AudioSessionSetActive(YES);
  107. //Audio::OpenALInit();
  108. //We can dealloc this whole delegate once we are done as the playback was asynchronous, which releases the movieController
  109. [self dealloc];
  110. }
  111. - (void)dealloc
  112. {
  113. //clean up
  114. //[movieController release];
  115. [movieViewController release];
  116. [super dealloc];
  117. }
  118. @end
  119. //C++ Section below
  120. bool playMovie(const char* fileName, const char* extension, MPMovieScalingMode scalingMode, MPMovieControlStyle controlStyle)
  121. {
  122. if((fileName != NULL) && (strlen(fileName) > 0))
  123. {
  124. //For now we assume the m4v format, as that is how iTunes exports it.
  125. // todo : make this try the other supported extensions, and or load without one. - Sven
  126. NSString* filePath = [[NSString alloc] initWithUTF8String:fileName];
  127. NSString* fileType = [[NSString alloc] initWithUTF8String:extension];
  128. iOSMoviePlaybackDelegate* playMovieDelegate = [iOSMoviePlaybackDelegate alloc];
  129. BOOL success = [playMovieDelegate playAVideo:filePath withFileType:fileType withScalingMode:scalingMode withControlStyle:controlStyle];
  130. if(success == NO)
  131. {
  132. Con::errorf("playiOSMovie : cannot find the file %s in the bundle", fileName);
  133. }
  134. [filePath release];
  135. //Continue with success
  136. return true;
  137. }
  138. return false;
  139. }
  140. //Make it accessible to scripts!
  141. ConsoleFunction(playiOSMovie, bool, 5, 5, "playiOSMovie(%filename, %filetype, %scaleMode, %controlMode) Returns True if the file was found, false otherwise")
  142. {
  143. MPMovieScalingMode scale = (MPMovieScalingMode)dAtoi(argv[3]);
  144. MPMovieControlStyle style = (MPMovieControlStyle)dAtoi(argv[4]);
  145. bool worked = playMovie(argv[1], argv[2], scale, style);
  146. return worked;
  147. }
  148. #endif //TORQUE_ALLOW_MOVIEPLAYER