Developer/iOS

[iOS9] AVPlayerViewController로 비디오 재생하기. Video player

블로blow 2016. 4. 7. 15:18
728x90

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


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];

    

}

728x90