iphone - 强制 MPMoviePlayerController 以横向模式播放视频

发布于 2024-10-13 01:16:06 字数 246 浏览 3 评论 0原文

我有一个仅纵向模式的应用程序,但是当用户播放视频时,我希望它以全屏横向模式播放(视频播放器在纵向模式下看起来不太好)。我是这样玩的:

[self.view addSubview:myMoviePlayer.view];
[self.myMoviePlayer setFullscreen:YES animated:YES];
[self.myMoviePlayer play];

完成这个任务的最佳方法是什么?

I have an app that is portrait mode only, but when the user plays a video I want it to play in fullscreen landscape mode (the video player doesn't look good in portrait mode). I'm playing it like this:

[self.view addSubview:myMoviePlayer.view];
[self.myMoviePlayer setFullscreen:YES animated:YES];
[self.myMoviePlayer play];

What's the best way to accomplish this?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧情勿念 2024-10-20 01:16:06

阅读这篇文章:

主要思想是:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

Read this article:

http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

The main idea is:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
错爱 2024-10-20 01:16:06

我的应用程序非常简单,使用 UINavigationController 作为根视图控制器,显示主导航屏幕和一些详细信息屏幕。由于布局限制,主屏幕不支持旋转,并且为了保持一致性,详细信息屏幕(简单地推送到导航控制器的堆栈上)也不支持旋转。但是,某些详细信息屏幕包含视频链接,并且视频需要以纵向模式显示。该应用程序使用 MPMoviePlayerController 以模态方式显示播放器。

我刚刚遇到了类似的问题,并尝试了上面的解决方案,该解决方案将仿射变换应用于 MPMoviePlayerController 的视图,但问题是状态栏继续以纵向模式显示,并与模式电影播放器​​视图重叠(在左侧,如果按照上面的旋转查看)。我尝试了一些方法但没有成功:

  • 隐藏状态栏。这不起作用,因为玩家存在于自己的世界中,无论如何都会继续显示状态栏。我找不到办法让它消失。

  • 显式设置状态栏方向。我不确定为什么这不起作用,但我怀疑这是因为我在 info.plist 中指出仅支持纵向,因此无法进行更改。

Net-net,以上对我不起作用。同样,b/c Apple 告诫开发人员将 MPMoviePlayerController 视为不透明,并且(特别是使用转换方法)我违反了这一点。

最后,我找到了一个对我有用的更简单的解决方案:

  1. ,我指出支持所有方向(除了颠倒,因此是标准的 iPhone 习惯用法)。

  2. 子类 UINavigationController,并重写适当的 shouldAutorotate 方法,以便仅支持纵向(请参阅 此解决方案,以及如何在 iOS <6 和 iOS6 中同时执行此操作)。

这是有效的,因为:

  1. 虽然我指出应用程序支持自动旋转,但我在包含我正在渲染的所有视图的 UINavigationController 子类处将其切断......所以一切都保持纵向,除了for:

  2. MPMoviePlayerController 是在 NavigationController 之上以模态方式呈现的,并且存在于它自己的世界中。因此,它可以自由地关注 info.plist 中的内容,并自行旋转。

有很多关于如何以模态方式呈现播放器的示例,但为了快速参考,这是我的代码:

- (void)presentModalMediaPlayerForURL:(NSURL *)inURL
{
    NSLog(@"Will play URL [%@]", [inURL description]);
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:inURL];
    [player.view setBounds:self.view.bounds];

    [player.moviePlayer prepareToPlay];
    [player.moviePlayer setFullscreen:YES animated:YES];
    [player.moviePlayer setShouldAutoplay:YES];
    [player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
    [self presentModalViewController:player animated:YES];

    [player release];
}

My app is quite simple, with a UINavigationController as the root view controller which displays a main navigation screen, and a few detail screens. The main screen does not support rotation due to layout constraints, and for consistency the detail screens (which are simply pushed onto the navcontroller's stack) don't either. However, some of the detail screens contain links to videos, and the videos need to display in portrait mode. The app displays the player modally using MPMoviePlayerController.

I just had a similar issue, and tried the solution above which applies an affine transform to the MPMoviePlayerController's view, but the problem was that the status bar continued to display as in portrait mode, and overlapped the modal movie player view (on the left, if viewed in according to the rotation above). I tried a couple of things to no avail:

  • Hiding the status bar. This didn't work, because the player exists in its own world, and goes ahead and presents the status bar anyway. I couldn't find a way to make this go away.

  • Explicitly setting the status bar orientation. I'm not sure exactly why this didn't work, but I suspect that it was because I'd indicated in my info.plist that only portrait was supported and was therefore cut off from making changes.

Net-net, the above didn't work for me. Just as well, b/c Apple admonishes devs to treat the MPMoviePlayerController as opaque, and (especially with the transform method) I was violating this.

In the end, I found a simpler solution that worked for me:

  1. In the info.plist, I indicated that all orientations (except for upside down, so the standard iPhone idiom) were supported.

  2. Subclass UINavigationController, and override the appropriate shouldAutorotate methods so that only Portrait is supported (see this solution, among others, for how to do this in iOS <6 and iOS6 concurrently).

This worked because:

  1. While I'd indicated that autorotation is supported by the app, I cut it off at the UINavigationController subclass which contains all of the views I'm rendering... so everything remains portrait, except for:

  2. The MPMoviePlayerController is being presented modally, atop the NavigationController, and lives in its own world. Therefore it's free to pay attention to what's in the info.plist, and rotate all by itself.

There are a bunch of examples for how to present the player modally, but for quick reference, here's my code:

- (void)presentModalMediaPlayerForURL:(NSURL *)inURL
{
    NSLog(@"Will play URL [%@]", [inURL description]);
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:inURL];
    [player.view setBounds:self.view.bounds];

    [player.moviePlayer prepareToPlay];
    [player.moviePlayer setFullscreen:YES animated:YES];
    [player.moviePlayer setShouldAutoplay:YES];
    [player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
    [self presentModalViewController:player animated:YES];

    [player release];
}
说谎友 2024-10-20 01:16:06

对于全屏播放,请使用 MPMoviePlayerViewController,然后要使其以横向格式启动和播放,请使用 MPMoviePlayerViewController 类上的“shouldAutorotateToInterfaceOrientation”方法。

它看起来像这样:

[yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];

For full screen playback use MPMoviePlayerViewController and then to make it launch and play in landscape format use the "shouldAutorotateToInterfaceOrientation" method on the MPMoviePlayerViewController class.

It looks like this:

[yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
吃颗糖壮壮胆 2024-10-20 01:16:06

我遇到了同样的情况,但我使用的是 iOS 6 和基于 NavController 的项目。有趣的是托管 MPMoviePlayerController 的 ViewController 我不想旋转,但我希望其中的视频旋转。

带有旋转 MPMoviePlayerController 的 ViewController

我只是根据设备方向根据需要手动旋转 MPMoviePlayerController。 _videoView 是 ViewController 的 MPMoviePlayerController 属性。例如,我只是将所需的宽度和高度硬编码为 16x9 的宽高比,因为我打算将该视频上传到 youtube。

- (void)updateVideoRotationForCurrentRotationWithAnimation:(bool)animation
{
    CGSize containerSize  = _videoView.frame.size;   // Container NOT rotated!
    float  videoWidth     = 384;                     // Keep 16x9 aspect ratio
    float  videoHeight    = 216;

    if( animation )
    {
        [UIView beginAnimations:@"swing" context:nil];
        [UIView setAnimationDuration:0.25];
    }

    switch( self.interfaceOrientation )
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(90));

            // This video player is rotated so flip width and height, but the container view
            // isn't rotated!
            [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoHeight)/2,
                                                    (containerSize.height-videoWidth)/2,
                                                    videoHeight,
                                                    videoWidth)];
            break;

        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(0));

            // This video player isn't rotated
            [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoWidth)/2,
                                                    (containerSize.height-videoHeight)/2,
                                                    videoWidth,
                                                    videoHeight)];
            break;
    }

    if( animation )
        [UIView commitAnimations];

}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self updateVideoRotationForCurrentRotationWithAnimation:YES];
}

我还在视图出现后调用 updateVideoRotationForCurrentRotationWithAnimation ,以便它具有正确的初始方向。

I had the same situation, but I am using iOS 6 and a NavController based project. What makes that interesting is the ViewController that is hosting the MPMoviePlayerController I don't want rotated, but I wanted the video within it to be rotated.

ViewController with rotated MPMoviePlayerController

I just manually rotate the MPMoviePlayerController as needed based on the devices orientation. _videoView is a MPMoviePlayerController property of the ViewController. For the example, I just hard coded the desired width and height to a 16x9 aspect ratio as I intend this video to be uploaded to youtube.

- (void)updateVideoRotationForCurrentRotationWithAnimation:(bool)animation
{
    CGSize containerSize  = _videoView.frame.size;   // Container NOT rotated!
    float  videoWidth     = 384;                     // Keep 16x9 aspect ratio
    float  videoHeight    = 216;

    if( animation )
    {
        [UIView beginAnimations:@"swing" context:nil];
        [UIView setAnimationDuration:0.25];
    }

    switch( self.interfaceOrientation )
    {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(90));

            // This video player is rotated so flip width and height, but the container view
            // isn't rotated!
            [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoHeight)/2,
                                                    (containerSize.height-videoWidth)/2,
                                                    videoHeight,
                                                    videoWidth)];
            break;

        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(0));

            // This video player isn't rotated
            [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoWidth)/2,
                                                    (containerSize.height-videoHeight)/2,
                                                    videoWidth,
                                                    videoHeight)];
            break;
    }

    if( animation )
        [UIView commitAnimations];

}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self updateVideoRotationForCurrentRotationWithAnimation:YES];
}

I also call updateVideoRotationForCurrentRotationWithAnimation after view did appear so it has the correct initial orientation.

时间海 2024-10-20 01:16:06

我使用以下代码仅使用 Movieplayer 的横向模式。

NSURL *movieURL = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];  // sample url
MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

// Logic for play movie in landscape
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieController.moviePlayer.view setTransform: landscapeTransform];

[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer prepareToPlay];
[movieController.moviePlayer play];

I use the following code to work with ONLY LANDSCAPE mode for Movieplayer.

NSURL *movieURL = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];  // sample url
MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

// Logic for play movie in landscape
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieController.moviePlayer.view setTransform: landscapeTransform];

[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer prepareToPlay];
[movieController.moviePlayer play];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文