【问题标题】:MediaPlayer MPMoviePlayerController - Not playing video in LandscapeMediaPlayer MPMoviePlayerController - 不在横向播放视频
【发布时间】:2012-03-16 06:00:00
【问题描述】:

真的希望有人能提供帮助,我可以通过单击按钮让我的视频播放,但它们总是以纵向模式播放,我真的希望它们以全屏横向播放。

这是我在这里使用的代码,我从这里添加了一些代码,有人说在他们的情况下修复了它,但毫无疑问在某种程度上弄乱了语法。

请有人让我摆脱痛苦并告诉我我做错了什么明显的事情。

#import "SecondViewController.h"


@implementation SecondViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BG.png"]];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}

-(IBAction)playMovie:(id)sender
{
NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"Allerjen Exclusive" ofType:@"mp4"];
NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]   initWithContentURL:fileURL];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];

[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;

UIView * playerView = [moviePlayerController view];
[playerView setFrame: CGRectMake(0, 0, 480, 320)];

CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);

[playerView setTransform: landscapeTransform];

moviePlayerController.fullscreen = TRUE;
moviePlayerController.controlStyle = MPMovieControlStyleFullscreen;

[moviePlayerController play];


}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayerController];

[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}


@end

【问题讨论】:

  • 请帮助:(我真的无法解决这个问题
  • 我面临同样的问题。已经尝试了那里的所有帖子。没有任何帮助

标签: iphone ios ios4 ios5


【解决方案1】:

使用 MPMoviePlayerViewController 代替 MPMoviePlayerController。它将处理方向、控制样式等。我提到了https://stackoverflow.com/a/4911172/3346048

MPMoviePlayerViewController *playerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:playerView];

并在 AppDelegate.m 中声明以下函数。这将在所有情况下将方向限制为纵向,并且仅在播放视频时才会改变

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{ if ([[self.window.rootViewController presentViewController] isKindOfClass:[MPMoviePlayerViewController class]]) { 返回 UIInterfaceOrientationMaskAll; } 别的 { //NSLog(@"方向应该改变"); 返回 UIInterfaceOrientationMaskPortrait; }

}

【讨论】:

  • 请告诉我拒绝投票的原因
【解决方案2】:

我认为当方向改变时你需要改变moviePlayer视图的框架, 使用以下函数:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
/* Size movie view to fit parent view. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],20,20);
[[[self moviePlayerController] view] setFrame:viewInsetRect];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-29
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多