在MPMoviePlayerController上呈现模态会导致崩溃,但仅限于iOS< 5

时间:2012-01-21 00:29:35

标签: iphone objective-c ios mpmovieplayercontroller modal-dialog

所以,这就是情况(这完全在模拟器中):

  • 创建一个视图控制器。它拥有一个MPMoviePlayerController,并将玩家的视图作为子视图添加到自己的视图中。
  • 将此vc设置为窗口的rootViewController。
  • 此vc视图中还有一个按钮,用于在暂停电影播放器​​后启动模态视图控制器(遮挡电影)。
  • 点击按钮,启动模态VC,然后关闭它。
  • 注意到电影播放器​​的暂停图像消失了(它只是黑色)。
  • 点击“播放”继续播放电影
  • 碰撞

现在,将您的构建目标从iOS 4.3更改为iOS 5.0,它可以完美运行。

真正令我困扰的是,这并不会导致一个月前的4.3下崩溃。事实上,我现在在商店里有一个应用程序可以正常工作:http://itunes.apple.com/us/app/es-musica-free/id474811522?mt=8

所以,我试图将其归结为一个可以重现问题的最小例子。

  • 创建一个vanilla xcode项目。

  • 你的AppDelegate.m中的


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        self.window.backgroundColor = [UIColor blackColor];
        [self.window makeKeyAndVisible];

        UIViewController *vc = [[DemoMoviePlayerViewController alloc] init];
        [self.window setRootViewController:vc];
        [vc release];
        vc = nil;

        return YES;
    }

  • DemoMoviePlayerViewController非常简单。这是h文件:


    #import 
    #import 

    @interface DemoMoviePlayerViewController : UIViewController
    {
        MPMoviePlayerController *moviePlayer_;
    }

    @end

  • 和m文件:


    #import "DemoMoviePlayerViewController.h"


    @interface DemoMoviePlayerViewController (InternalMethods)
    - (void)_buttonPressed:(UIButton*)button;
    @end


    @implementation DemoMoviePlayerViewController


    - (id)init
    {
        self = [super init];
        if (self == nil)
        {
            return nil;
        }

        [self setWantsFullScreenLayout:YES];

        NSURL *url = [NSURL URLWithString:@"http://once.unicornmedia.com/now/od/auto/0116d5af-bdc1-456d-a3ac-e3cbe25bac98/c762a821-e583-420c-b798-7b8c66736027/6c0e77e0-6dc4-48d5-9197-26e59271e8dd/content.once"];
        moviePlayer_ = [[MPMoviePlayerController alloc] initWithContentURL:url];

        return self;
    }


    - (void)dealloc
    {
        [moviePlayer_ stop];
        [moviePlayer_ release];
        moviePlayer_ = nil;

        [super dealloc];
    }


    #pragma mark - View lifecycle


    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [[self view] setBackgroundColor:[UIColor redColor]]; // debug

        [[moviePlayer_ view] setFrame:[[self view] bounds]];
        [[moviePlayer_ view] setAutoresizingMask:(UIViewAutoresizingFlexibleWidth
                                                  |UIViewAutoresizingFlexibleHeight)];
        [moviePlayer_ setControlStyle:MPMovieControlStyleFullscreen];
        [[moviePlayer_ view] setBackgroundColor:[UIColor greenColor]]; // debug
        [[self view] addSubview:[moviePlayer_ view]];

        // add a button to launch share kit
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setTitle:@"launch modal" forState:UIControlStateNormal];
        [button addTarget:self
                   action:@selector(_buttonPressed:)
         forControlEvents:UIControlEventTouchUpInside];

        [[moviePlayer_ view] addSubview:button];
        [button setFrame:CGRectMake(20, 100, 200, 40)];

        [moviePlayer_ setShouldAutoplay:YES];
        [moviePlayer_ play];
    }


    #pragma mark - internal methods -


    - (void)_dismissModal
    {
        [self dismissModalViewControllerAnimated:YES];
    }


    - (void)_buttonPressed:(UIButton*)button
    {
        if ([moviePlayer_ currentPlaybackRate] == 1.0)
        {
            [moviePlayer_ setShouldAutoplay:NO];
            [moviePlayer_ pause];
        }

        [self performSelector:@selector(_dismissModal) withObject:nil afterDelay:3.0];

        UIViewController *vc = [[UIViewController alloc] init];
        [[vc view] setBackgroundColor:[UIColor blueColor]];
        [self presentModalViewController:vc animated:YES];
        // yeah I know, vc is leaked...
    }


    @end

显然,尽管我的视图控制器在viewWillDisappear或viewDidDisappear中没有做任何事情,但MPMoviePlayerController在某种程度上能够以某种方式检测到它的视图被遮挡了。

同样,真正困扰我的是,据我所知,这个工作在一个月前的4.3下工作得很好。

0 个答案:

没有答案