간단하게 비디오만 재생시키는 샘플입니다.


1. .h 파일

@property (strong, nonatomic) NSURL *videoURL;

@property (strong, nonatomic) AVPlayerViewController *avVideoController;


2. Play Video

-(void) playMovie{

    

    self.avVideoController = [[AVPlayerViewController alloc] init];

    AVPlayer *player = [AVPlayer playerWithURL:[self localMovieURL]];

    self.avVideoController.player = player;

    

    [self.avVideoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [self.view addSubview:self.avVideoController.view];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayBackDidFinish:)

    name:AVPlayerItemDidPlayToEndTimeNotification

    object:[self.avVideoController.player currentItem]];

    

    [player play];

    

}


-(NSURL *)localMovieURL

{

    NSURL *theMovieURL = nil;

    NSBundle *bundle = [NSBundle mainBundle];

    if (bundle)

    {

        NSString *moviePath = [bundle pathForResource:@"IMG_0075" ofType:@"MOV"];

        if (moviePath)

        {

            theMovieURL = [NSURL fileURLWithPath:moviePath];

        }

    }

    return theMovieURL;

}


3. DidFinishNotification

- (void)videoPlayBackDidFinish:(NSNotification *)notification {

    

    [[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

    

    [self.avVideoController.view removeFromSuperview];

    self.avVideoController = nil;

    

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Video Playback" message:@"Just finished the video playback. The video is now removed." preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okayAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:okayAction];

    [self presentViewController:alertController animated:YES completion:nil];

    

}


WRITTEN BY
블로blow
iOS 개발자 생활이야기

트랙백  0 , 댓글  1개가 달렸습니다.
  1. 잘보고갑니다 2016.04.08 10:02
    좋은정보 잘 배워갑니다 감사합니다
secret