views:

267

answers:

3

I am trying to load a short movie file (.m4v) before the application starts However, when I using the ipad simulator, only the sound plays. There is no video running. If I change the simulator to iphone simulator, the video plays just fine.

I use MPMoviePlayerController to play the movie file

I even downloaded the sample mediaPlayer from Apple, and the result is the same. With the sample movie file (.m4v) the iphone simulator can play the movie, but the ipad simulator cannot

Please help!

A: 

Almost certain this is a know bug/limitation in the simulator.

Dad
So you meant the movie should play in iPad device?
Leo
Yes, exactly. Have you tried it on a device?
Dad
Eventually I figured it out, my implementation is not right.
Leo
A: 

What was your implementation mistake? Please share.

B-Money
A: 

Here is my solution if you need it: The solution is integrated with cocos2d, but it should be relatively easy to modify it regardless.

Please refer to the following website for general usage:

Getting MPMoviePlayerController to Work with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK

iPad iOS: 3.2.2

@implementation MovieLayer

/*
 * name: movie file name
 * type: movie file type
 * target: target class to handle the selectors
 * finish: selector called when the movie is finished
 * load: selector called when the movie loading state is changed
 */
+ (id)layerWithMovieName:(NSString*)name type:(NSString*)movieType target:(id)target 
    finish:(SEL)finishSelector load:(SEL)loadSelector
{
 return [[[self alloc] initWithMovieName:name type:movieType target:target 
          finish:finishSelector load:loadSelector] autorelease];
}

- (id)initWithMovieName:(NSString*)name type:(NSString*)movieType target:(id)target 
     finish:(SEL)finishSelector load:(SEL)loadSelector
{
 if ((self = [super init]) != nil)
 {   
  NSBundle *bundle = [NSBundle mainBundle];
  NSString *moviePath = [bundle pathForResource:name ofType:movieType];
  if (moviePath)
  {
   NSURL *moviePathURL = [NSURL fileURLWithPath:moviePath];
   [self loadMovieAtURL:moviePathURL];

   // Movie finish loading notification
   [[NSNotificationCenter defaultCenter]
    addObserver:target
    selector:loadSelector
    name:MPMoviePlayerLoadStateDidChangeNotification
    object:nil];

   // Movie finish Notification
   [[NSNotificationCenter defaultCenter] 
    addObserver:target 
    selector:finishSelector
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:nil];
  } 
 }
 return self;
}

- (void)dealloc
{

 [theMovie.view removeFromSuperview];
 theMovie.initialPlaybackTime = -1;
 [theMovie stop];
 [theMovie release];

 [super dealloc];
}

- (void)loadMovieAtURL:(NSURL*)theURL
{
 CGSize size = [[CCDirector sharedDirector] winSize];

 theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];

 [theMovie prepareToPlay];

 // > 3.2
 [theMovie respondsToSelector:@selector(loadState)];

 theMovie.scalingMode = MPMovieScalingModeAspectFill;
 [theMovie setFullscreen:TRUE animated:TRUE];
 theMovie.controlStyle = MPMovieControlStyleNone;

 theMovie.view.frame = CGRectMake(0, 0, size.width, size.height); 
 theMovie.view.backgroundColor = [UIColor clearColor];

 // Transform
 theMovie.view.transform = CGAffineTransformMakeRotation(-270.0f * (M_PI/180.0f));
 theMovie.view.center = [[CCDirector sharedDirector] openGLView].center;

 [[[CCDirector sharedDirector] openGLView] addSubview:theMovie.view]; 
}

- (void)play
{
 [theMovie play];
}

@end

Basic usage using cocos2d

MovieLayer *player = [MovieLayer layerWithMovieName:LOGO_ANIMATION 
                type:LOGO_ANIMATION_FILE_EXT 
              target:self
              finish:@selector(myMovieFinishedCallback:)
                load:@selector(myMovieLoadCallback:)];
  [self addChild:player z: 0 tag:1];
 }
 return self;
}

- (void)myMovieLoadCallback:(NSNotification*)notification
{
 MPMovieLoadState state = [(MPMoviePlayerController*)notification.object loadState];

 // Remove observer
 [[NSNotificationCenter defaultCenter]
  removeObserver:self
  name:MPMoviePlayerLoadStateDidChangeNotification
  object:nil];

 if (state & MPMovieLoadStateUnknown)
 {
  [self myMovieFinishedCallback:nil];
 }
 else if (state & MPMovieLoadStatePlayable)
 {
  [(MovieLayer*)[self getChildByTag:1] play];
 }
 else if (state & MPMovieLoadStatePlaythroughOK)
 {
  [(MovieLayer*)[self getChildByTag:1] play];
 }
 else if (state & MPMovieLoadStateStalled)
 {
  [self myMovieFinishedCallback:nil];
 }
}

- (void)myMovieFinishedCallback:(NSNotification*)notification
{ 
 [[NSNotificationCenter defaultCenter]
  removeObserver:self
  name:MPMoviePlayerPlaybackDidFinishNotification
  object:nil];

 [self removeChildByTag:1 cleanup:YES];

}
Leo