Yi Xue
Yi Xue

Reputation: 1

How to stop/playback the video

#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

Guys, I have no idea why the viewWillDisappear and viewWillAppear loop is not working. I want to stop the video if I scroll and keep playing when I scroll back. Let me know why it is not working. Btw, I don't know why the program terminates when the video is finished. I have used same code in the previous project and showed no error. I get bit confused. Hope some1 can help me. Thanks in advance.

Upvotes: 0

Views: 810

Answers (1)

Rama Rao
Rama Rao

Reputation: 1033

U gave scrollView.contentSize = CGSizeMake(4096, 768); This means that the view will not disappear or appear when u scroll.The scroll will position wise disappear or appear.Actually scroll is on view.So U need to manupulate the video stop and play when u scroll.So,u give the frame for player and when the scroll moving position appears the player position then play it.and when crosses the player frame stop it.Do this play and stop code in scrollViewDidScroll method with if else condition.

Upvotes: 1

Related Questions