多个 MPMoviePlayerViewController 全屏电影初始化和播放切换问题 - 全屏视图不会自动旋转以及类似的小问题

发布于 2024-09-02 07:30:02 字数 4733 浏览 2 评论 0原文

这是我在 SO 的第一篇文章。我要感谢所有 SO 用户,他们的贡献在我的所有项目中给了我很大帮助。我是 iPhone 应用程序开发领域的新手。

该项目的目标是在电影播放器​​对象(设备上的 *.mov 文件)之上拥有一个自定义交互式用户界面 (UI)。底层的全屏电影播放器​​可以不止一个,并且需要根据用户交互切换到不同的播放器。

我正在使用 cocos2D 来实现自定义交互式 UI 和效果(例如滑动手势上的粒子效果)。我正在使用多个 MPMoviePlayerViewController* 在全屏模式下播放驻留电影文件 (*.mov)(参考 MoviePlayer 示例)。

要求是通过在触摸检测到时在默认和备用 moviePlayer 之间切换来在电影之间进行切换。此外,切换应尽可能平滑。

为了实现平滑切换,我使用了两个 moviePlayerViewControllers,每个都是 AppController 中 UIView 对象的子视图。

问题a:我不确定这是否是实现电影切换的正确方法。

问题b:在我当前的解决方案中。默认 MoviePlayer 在纵向模式下旋转,有时 moviePlayer 对象不可见。行为并不一致。

问题 c:备用电影播放器​​(MPMoviePlayerViewController 的对象,首先作为 UIWindow 的子视图添加)在设备旋转时旋转,并且在默认电影播放器​​(MPMoviePlayerViewController 的对象之后添加)上正常运行)没有。想不出它的逻辑解释。我在某处读到 MPMoviePlayerViewController 创建了自己的窗口,这可能是一个问题。

    // From AppController.h
    UIWindow *window; // Parent application window
    UIView *viewCocos2D;    // Cocos View
    UIView *viewMovie;      // Default MoviePlayer View
    UIView *viewMovieAlternate;  // Alternate MoviePlayer View

// From AppController.m
// From DidAppFinishLoading
{
... 
 // Init the window
 window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

        // Init the views (Cocos2d, MOviePlayer and AlternateMOviePlayer)
 viewCocos2D = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 viewMovie = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 viewMovieAlternate = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

 // cocos2d will inherit these values
 [viewCocos2D setUserInteractionEnabled:YES]; 
 [viewCocos2D setMultipleTouchEnabled:YES];


 // create OpenGL view and attach it to a window
 //[[[UIApplication sharedApplication] keyWindow] addSubview:window];
 [[CCDirector sharedDirector] attachInView:viewCocos2D];

 // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 


 // Create a 
 customMoviePlayerWithUI = [[CustomMoviePlayerWithUI alloc] initWithSuperView:window andMovieView:viewMovie andAlternateView:viewMovieAlternate andSelf:self];
 CCScene *scene = [CCScene node];
 CocosCustomLayer *cocosLayer = [customMoviePlayerWithUI cocosLayer]; 


 [window addSubview:viewMovieAlternate];
 [window addSubview:viewMovie];
 // somehow this order lets cocos2D receive the touch events
 [window addSubview:viewCocos2D]; 


 [scene addChild: cocosLayer];
 [window makeKeyAndVisible];

 [[CCDirector sharedDirector] runWithScene: scene];
 [UIAccelerometer sharedAccelerometer].delegate = self;


 [customMoviePlayerWithUI start];
 [self didRotate:nil];

 ...
}

所以视图层次结构是

备用电影播放器​​视图位于底部 接下来是默认电影播放器​​视图 Cocos2D 图层视图位于顶部——Cocos2D 视图还接收所有事件。

    // From CustomMoviePlayerWithUI.h
 MPMoviePlayerViewController *moviePlayerView;
 MPMoviePlayerViewController *moviePlayerViewAlternate; 

// These delegates are used frequently to invoke some functions
 AppController* delegateApplication;
 UIView  *delegateSuperWindow;  // Window which contains everything - 
 UIView *delegateView;         // View containing default MoviePlayer
 UIView *delegateViewAlternate;// View containing alternate MoviePlayer

以下是 moviePlayerViewController 初始化代码 - 基本上它分配并初始化两个 moviePlayerViewController 对象,并将它们作为子视图添加到 AppController 中的默认和备用 UIView 对象

// From customMoviePlayer.m
// initAndPlayMovie is called from the init of CustomMoviePlayer
- (void)initAndPlayMovie:(UIView *)view andAlternateView:(UIView*) viewMovieAlternate
{
 // Initialize a movie player object with the specified URL
 moviePlayerView = [[MPMoviePlayerViewController alloc] init];
 moviePlayerViewAlternate = [[MPMoviePlayerViewController alloc] init];


 [moviePlayerView shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
 [moviePlayerViewAlternate shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];

 //if (moviePlayer)
 if (moviePlayerView && moviePlayerViewAlternate)
 {
  [[[moviePlayerView moviePlayer] backgroundView] setBackgroundColor:[UIColor blueColor]];
  [[[moviePlayerViewAlternate moviePlayer] backgroundView] setBackgroundColor:[UIColor redColor]];





  //[moviePlayerView setWantsFullScreenLayout:YES];

  // private API call.. don't use it..
  //[mp setOrientation:UIDeviceOrientationPortrait animated:NO];


  [view addSubview:[moviePlayerView view]];
  [viewMovieAlternate addSubview:[moviePlayerViewAlternate view]];
  //[view bringSubviewToFront:[moviePlayerView view]];

  //[[moviePlayerView moviePlayer] play]; 
 }
}

感谢您的所有帮助。如果您需要更多详细信息,请告诉我。

This is my first post at SO. I would like to thank all of the SO users, their contribution have helped me a lot in all of my projects. I am new to the iPhone Application development scene.

Aim of the project is to have a custom interactive User Interface (UI) on top of Movie Player objects (*.mov files on device). The underlying FullScreen MoviePlayers can be more than one and need to switch to different ones based on user Interaction.

I am using cocos2D to achieve the custom interactive UI and effects (for e.g. particle effects on swipe gesture). And I am using Multiple MPMoviePlayerViewController* to play the resident movie files (*.mov) on fullscreen mode (refered to MoviePlayer sample).

Requirement is to switch between movies by switching between default and alternate moviePlayer upon touch detection. Also, the switching should be as smooth as possible.

To achieve smooth switching I am using two moviePlayerViewControllers, each a subview of UIView object in AppController.

Problem a: I am not sure if this is the right way to achieve movie switching.

Problem b: In my current solution. The Default MoviePlayer is rotated in portrait mode, and sometimes the moviePlayer object is not visible. The behaviour is not consistent.

Problem c: The Alternate movie player (object of MPMoviePlayerViewController and added first as subview of UIWindow) rotates on device rotation and behaves properly the Default Movie Player (object of MPMoviePlayerViewController added after) doesn't. Can't think of logical explanation of it. Somewhere I read that MPMoviePlayerViewController creates it's own window and that might be an issue.

    // From AppController.h
    UIWindow *window; // Parent application window
    UIView *viewCocos2D;    // Cocos View
    UIView *viewMovie;      // Default MoviePlayer View
    UIView *viewMovieAlternate;  // Alternate MoviePlayer View

// From AppController.m
// From DidAppFinishLoading
{
... 
 // Init the window
 window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

        // Init the views (Cocos2d, MOviePlayer and AlternateMOviePlayer)
 viewCocos2D = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 viewMovie = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 viewMovieAlternate = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

 // cocos2d will inherit these values
 [viewCocos2D setUserInteractionEnabled:YES]; 
 [viewCocos2D setMultipleTouchEnabled:YES];


 // create OpenGL view and attach it to a window
 //[[[UIApplication sharedApplication] keyWindow] addSubview:window];
 [[CCDirector sharedDirector] attachInView:viewCocos2D];

 // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 


 // Create a 
 customMoviePlayerWithUI = [[CustomMoviePlayerWithUI alloc] initWithSuperView:window andMovieView:viewMovie andAlternateView:viewMovieAlternate andSelf:self];
 CCScene *scene = [CCScene node];
 CocosCustomLayer *cocosLayer = [customMoviePlayerWithUI cocosLayer]; 


 [window addSubview:viewMovieAlternate];
 [window addSubview:viewMovie];
 // somehow this order lets cocos2D receive the touch events
 [window addSubview:viewCocos2D]; 


 [scene addChild: cocosLayer];
 [window makeKeyAndVisible];

 [[CCDirector sharedDirector] runWithScene: scene];
 [UIAccelerometer sharedAccelerometer].delegate = self;


 [customMoviePlayerWithUI start];
 [self didRotate:nil];

 ...
}

So the View hierarchy is

Alternate Movie Player View is at bottom
Default Movie Player view is next
Cocos2D Layer View is on top -- Cocos2D view also receives all of the events.

    // From CustomMoviePlayerWithUI.h
 MPMoviePlayerViewController *moviePlayerView;
 MPMoviePlayerViewController *moviePlayerViewAlternate; 

// These delegates are used frequently to invoke some functions
 AppController* delegateApplication;
 UIView  *delegateSuperWindow;  // Window which contains everything - 
 UIView *delegateView;         // View containing default MoviePlayer
 UIView *delegateViewAlternate;// View containing alternate MoviePlayer

Following is the moviePlayerViewController init code -- basically it allocs and inits two moviePlayerViewController objects and adds them as subview to the Default and Alternate UIView objects from AppController

// From customMoviePlayer.m
// initAndPlayMovie is called from the init of CustomMoviePlayer
- (void)initAndPlayMovie:(UIView *)view andAlternateView:(UIView*) viewMovieAlternate
{
 // Initialize a movie player object with the specified URL
 moviePlayerView = [[MPMoviePlayerViewController alloc] init];
 moviePlayerViewAlternate = [[MPMoviePlayerViewController alloc] init];


 [moviePlayerView shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
 [moviePlayerViewAlternate shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];

 //if (moviePlayer)
 if (moviePlayerView && moviePlayerViewAlternate)
 {
  [[[moviePlayerView moviePlayer] backgroundView] setBackgroundColor:[UIColor blueColor]];
  [[[moviePlayerViewAlternate moviePlayer] backgroundView] setBackgroundColor:[UIColor redColor]];





  //[moviePlayerView setWantsFullScreenLayout:YES];

  // private API call.. don't use it..
  //[mp setOrientation:UIDeviceOrientationPortrait animated:NO];


  [view addSubview:[moviePlayerView view]];
  [viewMovieAlternate addSubview:[moviePlayerViewAlternate view]];
  //[view bringSubviewToFront:[moviePlayerView view]];

  //[[moviePlayerView moviePlayer] play]; 
 }
}

Thanks for all of your help. Let me know if you need any more details on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文