2011-08-09 57 views
2

我有一些问题正确旋转MPMoviePlayerController的一个实例。我希望它基于iPad的方向旋转。通常情况下,它的工作。然而,有时候会发生一些奇怪的事情,然后它就被搞砸了,直到我重新启动程序。如何正确旋转MPMoviePlayerController?

这里是我的初始化代码:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Drew 320x240" ofType:@"mp4" inDirectory:nil]; 
myMPC = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; 

[[myMPC view] setFrame: CGRectMake(0, 0, 597, 448)]; 
myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(-90)); 
myMPC.view.center = CGPointMake(364, (850/2) + 168); 

这里是我的方向代码:

-(void)orientationDetected:(UIEvent *)event{ 
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) { 
     if (myMPC) { 
      myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(90)); 
      myMPC.view.center = CGPointMake(405, (850/2)); 
     } 
    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) { 
     if (myMPC) { 
      myMPC.view.transform = CGAffineTransformMakeRotation(SP_D2R(-90)); 
      myMPC.view.center = CGPointMake(364, (850/2) + 174); 
     } 
    } 
} 

这里有一些截图。前两个是他们应该是怎么样的(一​​个是home键离开了,另一个是home键右键),第三个是搞砸了。在这里,他们是:

Landscape Home Button Left

Landscape Home Button Right

Screw Up

正如你可以看到,男孩的视频移出的位置,并留下黑暗的地方应该是(虽然黑色矩形不是完全应该在哪里 - 在这个例子中它应该更靠左)。一旦发生这种情况,所有后续的设备旋转都会导致这种情况(直到程序重新启动)。

这不是一个模拟器特定的问题,因为这发生在实际的iPad上。再次,有时它旋转得很好。有什么我失踪/有人知道这是为什么发生/有人知道如何防止这种情况吗?

我很感激帮助。

谢谢 伊利亚

编辑:初始化代码添加到现在的照片被嵌入。

+0

找到更多的细节尽量与每一个旋转 –

+0

我只是想右前设置,然后之后便又设置框架 - 这是一个没有去。不管怎么说,还是要谢谢你。还有什么想法? – IlyaK

+0

我做了同样的Shaharyar指出... MPMoviePlayerViewController与presentModalViewController结合:动画: –

回答

1

我第一次使用MPMoviePlayerController时遇到了类似的问题。这个方向对我来说不起作用。

我发现是什么:MPMoviePlayerViewController(它包含一个MPMoviePlayerControlle

使用这个新的类,我曾与方向没有更多的问题。我还可以在shouldAutorotateToInterfaceOrientation的子类中定义自己的方向。 (感觉就像魔法:P)

记住,在MPMoviePlayerViewController类是由[self presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController)]

管理方向你现在正在做的方式是非常繁琐和容易出错的调用。

使用框架中的内置类可以轻松完成。

HTH

+0

谢谢你回到我身边。我会尽早回复,但我没有收到电子邮件通知......哦,好吧。 我只是想确保我正确理解你的答案:我应该继承MPMoviePlayerViewController,然后将方法shouldAutorotateToInterfaceOrientation添加到我的新类中?有什么我失踪?我的新课堂是否需要接收定位通知? 谢谢, Ilya – IlyaK

+0

是的,正如你所提到的 - 你只是在你的新的子类中重载shoulAutorotateToInterfaceOrientation并添加你支持的方向。 –

+0

好的。我会试试这个并回复你。 谢谢, 伊利亚 – IlyaK

0

我认为设置视频旋转的锚点可能会解决您的问题。

尝试初始化如下:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Drew 320x240" ofType:@"mp4" inDirectory:nil]; 
myMPC = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]]; 

[[myMPC view] setFrame: CGRectMake(0, 0, 597, 448)]; 
[[myMPC view].layer setAnchorPoint:CGPointMake(0.5, 0.5)]; // center of your view 

视频现在应该围绕其中心旋转。

可以在the Apple Docs here