如何停止/播放视频

时间:2012-04-05 21:50:56

标签: ios

#import "ViewController.h"

@implementation ViewController
@synthesize scrollView;

- (void)viewDidLoad
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(4096, 768);


    NSString *url = [[NSBundle mainBundle] pathForResource:@"F0" ofType:@"mov"];

    player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                          selector:@selector(movieFinishedCallback) 
                                          name:MPMoviePlayerPlaybackDidFinishNotification 
                                          object:player];
    player.view.frame = CGRectMake(0, 0, 1024, 768);
    player.scalingMode = MPMovieScalingModeAspectFill;
    [scrollView addSubview:player.view];
    [player play];

    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"29-30-p1.jpg"]];
    image1.frame = CGRectMake(1024,0,1024,768);
    image1.clipsToBounds = YES;
    [scrollView addSubview:image1];
    image1.animationImages = eyeFrames;
    image1.animationDuration = 0.25;
    image1.animationRepeatCount = 1;

    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"29-30-p2.jpg"]];
    image2.frame = CGRectMake(2048,0,1024,768);
    image2.clipsToBounds = YES;
    [scrollView addSubview:image2];

    UIImageView *image3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"29-30-p3.jpg"]];
    image3.frame = CGRectMake(3072,0,1024,768);
    image3.clipsToBounds = YES;
    [scrollView addSubview:image3];


    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void) movieFinishedCallback:(NSNotification *) aNotification{
     MPMoviePlayerController *moviePlayer = [aNotification object];
     [[NSNotificationCenter defaultCenter] removeObserver:self
                                           name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:moviePlayer];
    [moviePlayer.view removeFromSuperview];
    [player release];

}

-(void)viewWillDisappear:(BOOL)animated{

    //NSLog(@"x=%f",scrollView.contentOffset.x);
    [player stop];

}

-(void)viewWillAppear:(BOOL)animated{
    [player play];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

伙计们,我不知道为什么viewWillDisappearviewWillAppear循环不起作用。如果我滚动并在向后滚动时继续播放,我想停止视频。让我知道它为什么不起作用。顺便说一句,我不知道为什么程序在视频结束时终止。我在之前的项目中使用了相同的代码并且没有显示任何错误。我有点困惑。希望some1可以帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

U给了scrollView.contentSize = CGSizeMake(4096, 768);这意味着当你滚动时视图不会消失或出现。滚动将明智地消失或出现。实际上滚动在视图上。所以你需要在视频停止和播放时进行操作。所以,你给player的框架,当滚动移动位置出现时,玩家位置然后播放它。当穿过玩家框架时停止它。在scrollViewDidScroll中播放和停止代码使用if else条件的方法。

相关问题