IOS之征战MPMoviePlayerViewController视频播放(MediaPlayer)

视频播放 只需要调用playNativeMovie 方法传入一个 url路径字符串即可;

/**播放视频*/
- (void)playNativeMovie:(NSString *)playUrlStr
{
    NSURL * url = [NSURL URLWithString:playUrlStr];

    // 播放路径
    self.player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

    // 设置缩放类型
   // _player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

    // 设置控制器类型 MPMovieControlStyleDefault MPMovieControlStyleFullscreen
    _player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

    // 设置播放源类型
    _player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

    [self.view addSubview:_player.view];

    // 设定播放画面的大小
    _player.view.frame = self.view.layer.bounds;
    _player.view.backgroundColor = [UIColor whiteColor];

    // 模态化推出一个播放页面
    [self presentMoviePlayerViewControllerAnimated:_player];

    // 使用通知对播放进行监控
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    // 播放
    [_player.moviePlayer play];

}

/**播放完成*/
- (void)doFinished:(id)sender
{
    // 通知响应
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    // 将播放视图从父视图移除
    [_player.view removeFromSuperview];

    // 将MPMoviePlayerViewController置空
    _player = nil;

}

调用视频播放

- (void)viewDidLoad {
    [super viewDidLoad];

    [self playNativeMovie:@"http://vss.waqu.com/dusdcsuang6u5btu/normal.mp4"];

}

注意:如果你的url是 http 而不是 https 需要在 plist文件中设置一下
这里写图片描述

你可能感兴趣的:(ios,视频,视频播放器,mediaplayer)