视频无法在iPad模拟器中播放

时间:2010-07-01 07:01:05

标签: ipad cocos2d-iphone mpmovieplayercontroller

我正在尝试在应用程序启动之前加载一个短电影文件(.m4v) 但是,当我使用ipad模拟器时,只播放声音。没有视频正在运行。 如果我将模拟器更改为iphone模拟器,视频播放就可以了。

我使用MPMoviePlayerController播放电影文件

我甚至从Apple下载了样本mediaPlayer,结果是一样的。使用示例电影文件(.m4v),iphone模拟器可以播放电影,但ipad模拟器不能

请帮忙!

2 个答案:

答案 0 :(得分:3)

如果您需要,我的解决方案如下: 该解决方案与cocos2d集成,但无论如何都应该相对容易地修改它。

请参阅以下网站了解一般用法:

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

使用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];

}

答案 1 :(得分:0)

几乎可以肯定这是模拟器中的已知错误/限制。