Skip to content

Instantly share code, notes, and snippets.

@maleko
Created May 27, 2010 04:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maleko/415477 to your computer and use it in GitHub Desktop.
Save maleko/415477 to your computer and use it in GitHub Desktop.
We needed to autorotate the MPMoviePlayerViewController to play the movie in landscape (autorotating as well) whilst within the context of the CCDirector. Normally, the movie player would play in landscape only.
Bad thing about this solution is that it still flashes the movie player controls just for a moment before playing.
Otherwise it looks good. Any suggestions from anybody would be welcomed =).
//
// CocosiPadAppDelegate.m
// Cocos.iPad
//
// Created by Marc Lee on 19/04/10.
// jTribe Holdings Pty Ltd & Adviso Technologies 2010
//
#import "CocosiPadAppDelegate.h"
#import "cocos2d.h"
@implementation CocosiPadAppDelegate
@synthesize window;
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Tell the UIDevice to send notifications when the orientation changes
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// cocos2d will inherit these values
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
// Try to use CADisplayLink director
// if it fails (SDK < 3.1) use the default director
if( ! [CCDirector setDirectorType:CCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:CCDirectorTypeDefault];
[[CCDirector sharedDirector] setPixelFormat:kPixelFormatRGBA8888];
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
// before creating any layer, set the landscape mode
[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
[[CCDirector sharedDirector] setAnimationInterval:1.0/60];
//[[CCDirector sharedDirector] setDisplayFPS:YES];
// create an openGL view inside a window
[[CCDirector sharedDirector] attachInView:window];
[window makeKeyAndVisible];
[[CCDirector sharedDirector] runWithScene: [YourAwesomeScreen scene]];
// this suppresses the flicker at the beginning of the screne
// http://www.mailinglistarchive.com/html/cocos2d-iphone-discuss@googlegroups.com/2009-03/msg00484.html
[[CCDirector sharedDirector] mainLoop];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[CCDirector sharedDirector] end];
}
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
// tell the director that the orientation has changed
- (void) orientationChanged:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation:(ccDeviceOrientation) orientation];
}
}
- (void)dealloc {
[[CCDirector sharedDirector] release];
[window release];
[super dealloc];
}
@end
//
// MovieViewController.h
// Cocos.iPad
//
// Created by Marc Lee on 27/05/10.
// jTribe Holdings Pty Ltd & Adviso Technologies 2010
//
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import "cocos2d.h"
@interface MovieViewController : UIViewController {
MPMoviePlayerViewController *moviePlayer;
NSURL *movieURL;
CCLayer *scene;
}
@property (readwrite, retain) MPMoviePlayerViewController *moviePlayer;
@property (nonatomic, retain) NSURL *movieURL;
- (id)delegate;
- (void)setDelegate: (id)newDelegate;
- (void)initAndPlayMovie:(NSURL *)movieURL layer:(CCLayer *) layer;
@end
//
// MovieViewController.m
// Cocos.iPad
//
// Created by Marc Lee on 27/05/10.
// jTribe Holdings Pty Ltd & Adviso Technologies 2010
//
#import "MovieViewController.h"
@implementation MovieViewController
@synthesize moviePlayer;
@synthesize movieURL;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[moviePlayer moviePlayer]];
}
-(void)initAndPlayMovie:(NSURL *)mURL layer:(CCLayer*) layer
{
scene = layer;
//Initialize a movie player object with the specified URL
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:mURL];
mp.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mp shouldAutorotateToInterfaceOrientation:YES];
// save the movie play object
self.moviePlayer = mp;
[mp release];
[self presentMoviePlayerViewControllerAnimated:self.moviePlayer];
}
- (void)movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player stop];
[self dismissMoviePlayerViewControllerAnimated];
[scene movieFinishedCallback];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)dealloc {
[super dealloc];
}
@end
//
// YourAwesomeScene.h
// Cocos.iPad
//
// Created by Marc Lee on 25/05/10.
// jTribe Holdings Pty Ltd & Adviso Technologies 2010
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "MovieViewController.h"
@interface YourAwesomeScene : CCLayer {
MovieViewController *videoController;
}
@property (readwrite, retain) MovieViewController *videoController;
+(id) scene;
@end
//
// YourAwesomeScene.m
// Cocos.iPad
//
// Created by Marc Lee on 25/05/10.
// jTribe Holdings Pty Ltd & Adviso Technologies 2010
//
#import "YourAwesomeScene.h"
#import "SimpleAudioEngine.h"
int soundId;
@implementation YourAwesomeScene
#pragma mark -
#pragma mark Initialisation
+(id) scene {
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
YourAwesomeScene *layer = [YourAwesomeScene node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init {
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {
}
return self;
}
// blah blah blah your stuff here
- (void) playMovie:(id) sender {
NSLog(@"Button tapped");
[[SimpleAudioEngine sharedEngine] stopEffect:soundId];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"movieFileName" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
videoController = [[MovieViewController alloc] init];
[[[CCDirector sharedDirector] openGLView] addSubview:videoController.view];
[videoController initAndPlayMovie:movieURL layer:self];
}
#pragma mark -
#pragma mark Callbacks
- (void) movieFinishedCallback {
for (id subView in [[[CCDirector sharedDirector] openGLView] subviews]) {
[(UIView *) subView removeFromSuperview];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment