Play a video using mediaplayer

Source: Internet
Author: User
Play video with mediaplayer by Wu xueying
- (IBAction)userPlayerController:(id)sender {    if (_moviePlayer == nil) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished4MoviePlayerController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClick:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];                _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];                _moviePlayer.scalingMode = MPMovieScalingModeAspectFill;        _moviePlayer.controlStyle = MPMovieControlStyleFullscreen;                [self.view addSubview:_moviePlayer.view];    }    [_moviePlayer play];    [_moviePlayer setFullscreen:YES animated:YES];}- (void)playbackFinished4MoviePlayerController:(NSNotification *)notification {    [[NSNotificationCenter defaultCenter] removeObserver:self];    [_moviePlayer stop];    [_moviePlayer.view removeFromSuperview];    _moviePlayer = nil;}- (void)doneButtonClick:(NSNotification *)aNotification {    if (_moviePlayer.playbackState == MPMoviePlaybackStateStopped) {        [_moviePlayer.view removeFromSuperview];        _moviePlayer = nil;    }}- (IBAction)userPlayerViewController:(id)sender {    if (_moviePlayerView == nil) {        _moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:[self movieURL]];        _moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;        _moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished4MoviePlayerViewController:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];    }    [self presentMoviePlayerViewControllerAnimated:_moviePlayerView];}- (void)playbackFinished4MoviePlayerViewController:(NSNotification *)notification {    [[NSNotificationCenter defaultCenter] removeObserver:self];    [_moviePlayerView dismissMoviePlayerViewControllerAnimated];    _moviePlayerView = nil;}- (NSURL *)movieURL {    NSBundle *bundle = [NSBundle mainBundle];    NSString *moviePath = [bundle pathForResource:@"xx" ofType:@"mp4"];    if (moviePath) {        return [NSURL fileURLWithPath:moviePath];    }else {        return nil;    }}


Play a video using mediaplayer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.