iOS多视图和MPMoviePlayer问题

时间:2011-06-06 11:56:32

标签: iphone ios ipad ios4

我有两个视图控制器,一个是mainController,第二个是movieController ......

在movieController中使用[self.view removeFromSuperview];向后播放回mainController后,在movieController中播放的视频仍会播放为背景声音。

我确实发布了播放器,但不知道。

@synthesize viewForMovie,player;
- (void)viewDidLoad {
    [super viewDidLoad];
    //background image
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"mits_bg.png"]];

    //gesture recognizer -to up
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(handleSwipes:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeGesture];

    [swipeGesture release];

    //movie player
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(movieDurationAvailable:)
     name:MPMovieDurationAvailableNotification
     object:nil];

    **self.player = [[[MPMoviePlayerController alloc] init] autorelease];**
    //self.player.controlStyle = MPMovieControlStyleNone; // no control at all :)
    self.player.contentURL = [self movieURL];

    self.player.view.frame = self.viewForMovie.bounds;
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;

    [self.viewForMovie addSubview:player.view];
    [self.player play];
}

//---gesture recognizer
- (IBAction) handleSwipes:(UIGestureRecognizer *) sender {
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];

    if (direction == UISwipeGestureRecognizerDirectionDown) {

        CATransition *transition = [CATransition animation];
        transition.duration = 0.75;

        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

        transition.type = kCATransitionPush;
        transition.subtype = kCATransitionFromBottom;

        transition.delegate = self;

        // Next add it to the containerView's layer. This will perform the transition based on how we change its contents.
        [self.view.superview.layer addAnimation:transition forKey:nil];
        [self.view removeFromSuperview];
        **self.player = nil;**
        //[player release]; // I give it a try like this also

    }
}

- (void)viewDidUnload {
    [super viewDidUnload];
    self.viewForMovie = nil;
    self.player = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [thumbnailScrollView release];
    [viewForMovie release];
    [onScreenDisplayLabel release];
    [player release];
    [super dealloc];
}

1 个答案:

答案 0 :(得分:0)

执行此操作时,您将保留两次,

self.player = [[MPMoviePlayerController alloc] init];

您应该向此添加autorelease消息,

self.player = [[[MPMoviePlayerController alloc] init] autorelease];

除了viewDidUnloaddealloc之外,我没有看到被发布的版本。您应该在从superview中删除视图后立即调用它。 nil是一个更好的选择,因为dealloc中的发布调用将位于nil而非解除分配的对象上。因此,在从视图中删除它之后,将其nil设为这样,

self.player = nil;